Skip to content

Commit 1f5a9d6

Browse files
committed
Fixed some offenses.
1 parent d3aebd5 commit 1f5a9d6

File tree

5 files changed

+77
-85
lines changed

5 files changed

+77
-85
lines changed

.rubocop_todo.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2016-01-18 16:32:43 +0100 using RuboCop version 0.36.0.
3+
# on 2016-01-19 22:54:29 +0100 using RuboCop version 0.36.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -19,11 +19,10 @@ Lint/Debugger:
1919
Exclude:
2020
- 'spec/integration/common_validation.rb'
2121

22-
# Offense count: 8
22+
# Offense count: 7
2323
# Cop supports --auto-correct.
2424
Lint/DeprecatedClassMethods:
2525
Exclude:
26-
- 'lib/annotate.rb'
2726
- 'lib/annotate/annotate_routes.rb'
2827
- 'spec/fixtures/rails32_boot.rb'
2928
- 'spec/integration/rails_3.2.2/config/boot.rb'
@@ -379,7 +378,7 @@ Style/IfInsideElse:
379378
Exclude:
380379
- 'lib/annotate/annotate_routes.rb'
381380

382-
# Offense count: 7
381+
# Offense count: 6
383382
# Cop supports --auto-correct.
384383
# Configuration parameters: MaxLineLength.
385384
Style/IfUnlessModifier:
@@ -635,7 +634,7 @@ Style/SpaceAfterComma:
635634
Exclude:
636635
- 'lib/annotate/annotate_models.rb'
637636

638-
# Offense count: 58
637+
# Offense count: 50
639638
# Cop supports --auto-correct.
640639
Style/SpaceAfterControlKeyword:
641640
Exclude:
@@ -720,14 +719,14 @@ Style/SpaceInsideStringInterpolation:
720719
Style/SpecialGlobalVars:
721720
EnforcedStyle: use_perl_names
722721

723-
# Offense count: 403
722+
# Offense count: 373
724723
# Cop supports --auto-correct.
725724
# Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
726725
# SupportedStyles: single_quotes, double_quotes
727726
Style/StringLiterals:
728727
Enabled: false
729728

730-
# Offense count: 3
729+
# Offense count: 2
731730
# Cop supports --auto-correct.
732731
# Configuration parameters: EnforcedStyle, SupportedStyles.
733732
# SupportedStyles: single_quotes, double_quotes

bin/annotate

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

3-
unless File.exists?('./Rakefile') || File.exists?('./Gemfile')
4-
abort "Please run annotate from the root of the project."
3+
unless File.exist?('./Rakefile') || File.exist?('./Gemfile')
4+
abort 'Please run annotate from the root of the project.'
55
end
66

77
require 'rubygems'
@@ -20,58 +20,55 @@ Annotate.bootstrap_rake
2020

2121
has_set_position = {}
2222
target_action = :do_annotations
23+
positions = %w(before top after bottom)
2324

2425
OptionParser.new do |opts|
25-
opts.banner = "Usage: annotate [options] [model_file]*"
26-
27-
opts.on('-d', '--delete',
28-
"Remove annotations from all model files or the routes.rb file") do
26+
opts.banner = 'Usage: annotate [options] [model_file]*'
2927

28+
opts.on('-d', '--delete', 'Remove annotations from all model files or the routes.rb file') do
3029
target_action = :remove_annotations
3130
end
3231

33-
opts.on('-p', '--position [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
34-
"Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)") do |p|
32+
opts.on('-p', '--position [before|top|after|bottom]', positions,
33+
'Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s)') do |p|
3534
ENV['position'] = p
36-
[
37-
'position_in_class','position_in_factory','position_in_fixture','position_in_test', 'position_in_routes', 'position_in_serializer'
38-
].each do |key|
35+
%w(position_in_class position_in_factory position_in_fixture position_in_test position_in_routes position_in_serializer).each do |key|
3936
ENV[key] = p unless(has_set_position[key])
4037
end
4138
end
4239

43-
opts.on('--pc', '--position-in-class [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
44-
"Place the annotations at the top (before) or the bottom (after) of the model file") do |p|
40+
opts.on('--pc', '--position-in-class [before|top|after|bottom]', positions,
41+
'Place the annotations at the top (before) or the bottom (after) of the model file') do |p|
4542
ENV['position_in_class'] = p
4643
has_set_position['position_in_class'] = true
4744
end
4845

49-
opts.on('--pf', '--position-in-factory [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
50-
"Place the annotations at the top (before) or the bottom (after) of any factory files") do |p|
46+
opts.on('--pf', '--position-in-factory [before|top|after|bottom]', positions,
47+
'Place the annotations at the top (before) or the bottom (after) of any factory files') do |p|
5148
ENV['position_in_factory'] = p
5249
has_set_position['position_in_factory'] = true
5350
end
5451

55-
opts.on('--px', '--position-in-fixture [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
56-
"Place the annotations at the top (before) or the bottom (after) of any fixture files") do |p|
52+
opts.on('--px', '--position-in-fixture [before|top|after|bottom]', positions,
53+
'Place the annotations at the top (before) or the bottom (after) of any fixture files') do |p|
5754
ENV['position_in_fixture'] = p
5855
has_set_position['position_in_fixture'] = true
5956
end
6057

61-
opts.on('--pt', '--position-in-test [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
62-
"Place the annotations at the top (before) or the bottom (after) of any test files") do |p|
58+
opts.on('--pt', '--position-in-test [before|top|after|bottom]', positions,
59+
'Place the annotations at the top (before) or the bottom (after) of any test files') do |p|
6360
ENV['position_in_test'] = p
6461
has_set_position['position_in_test'] = true
6562
end
6663

67-
opts.on('--pr', '--position-in-routes [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
68-
"Place the annotations at the top (before) or the bottom (after) of the routes.rb file") do |p|
64+
opts.on('--pr', '--position-in-routes [before|top|after|bottom]', positions,
65+
'Place the annotations at the top (before) or the bottom (after) of the routes.rb file') do |p|
6966
ENV['position_in_routes'] = p
7067
has_set_position['position_in_routes'] = true
7168
end
7269

73-
opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', ['before', 'top', 'after', 'bottom'],
74-
"Place the annotations at the top (before) or the bottom (after) of the serializer files") do |p|
70+
opts.on('--ps', '--position-in-serializer [before|top|after|bottom]', positions,
71+
'Place the annotations at the top (before) or the bottom (after) of the serializer files') do |p|
7572
ENV['position_in_serializer'] = p
7673
has_set_position['position_in_serializer'] = true
7774
end
@@ -89,34 +86,32 @@ OptionParser.new do |opts|
8986
ENV['wrapper_close'] = p
9087
end
9188

92-
opts.on('-r', '--routes',
93-
"Annotate routes.rb with the output of 'rake routes'") do
89+
opts.on('-r', '--routes', "Annotate routes.rb with the output of 'rake routes'") do
9490
ENV['routes'] = 'true'
9591
end
9692

9793
opts.on('-v', '--version',
98-
"Show the current version of this gem") do
94+
'Show the current version of this gem') do
9995
puts "annotate v#{Annotate.version}"; exit
10096
end
10197

102-
opts.on('-m', '--show-migration',
103-
"Include the migration version number in the annotation") do
104-
ENV['include_version'] = "yes"
98+
opts.on('-m', '--show-migration', 'Include the migration version number in the annotation') do
99+
ENV['include_version'] = 'yes'
105100
end
106101

107102
opts.on('-k', '--show-foreign-keys',
108103
"List the table's foreign key constraints in the annotation") do
109-
ENV['show_foreign_keys'] = "yes"
104+
ENV['show_foreign_keys'] = 'yes'
110105
end
111106

112107
opts.on('-i', '--show-indexes',
113108
"List the table's database indexes in the annotation") do
114-
ENV['show_indexes'] = "yes"
109+
ENV['show_indexes'] = 'yes'
115110
end
116111

117112
opts.on('-s', '--simple-indexes',
118113
"Concat the column's related indexes in the annotation") do
119-
ENV['simple_indexes'] = "yes"
114+
ENV['simple_indexes'] = 'yes'
120115
end
121116

122117
opts.on('--model-dir dir',
@@ -131,17 +126,17 @@ OptionParser.new do |opts|
131126

132127
opts.on('--ignore-model-subdirects',
133128
"Ignore subdirectories of the models directory") do |dir|
134-
ENV['ignore_model_sub_dir'] = "yes"
129+
ENV['ignore_model_sub_dir'] = 'yes'
135130
end
136131

137132
opts.on('--sort',
138133
"Sort columns alphabetically, rather than in creation order") do |dir|
139-
ENV['sort'] = "yes"
134+
ENV['sort'] = 'yes'
140135
end
141136

142137
opts.on('--classified-sort',
143138
"Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns") do |dir|
144-
ENV['classified_sort'] = "yes"
139+
ENV['classified_sort'] = 'yes'
145140
end
146141

147142
opts.on('-R', '--require path',
@@ -155,10 +150,10 @@ OptionParser.new do |opts|
155150

156151
opts.on('-e', '--exclude [tests,fixtures,factories,serializers]', Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions|
157152
exclusions ||= %w(tests fixtures factories)
158-
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = "yes" }
153+
exclusions.each { |exclusion| ENV["exclude_#{exclusion}"] = 'yes' }
159154
end
160155

161-
opts.on('-f', '--format [bare|rdoc|markdown]', ['bare', 'rdoc', 'markdown'], 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
156+
opts.on('-f', '--format [bare|rdoc|markdown]', %w(bare rdoc markdown), 'Render Schema Infomation as plain/RDoc/Markdown') do |fmt|
162157
ENV["format_#{fmt}"] = 'yes'
163158
end
164159

@@ -186,8 +181,8 @@ OptionParser.new do |opts|
186181
ENV['hide_limit_column_types'] = "#{values}"
187182
end
188183

189-
opts.on('--ignore-unknown-models', "don't display warnings for bad model files" ) do |values|
190-
ENV['ignore_unknown_models'] = "true"
184+
opts.on('--ignore-unknown-models', "don't display warnings for bad model files" ) do |_values|
185+
ENV['ignore_unknown_models'] = 'true'
191186
end
192187

193188
end.parse!

lib/annotate.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,9 @@ def self.bootstrap_rake
163163
end
164164
require 'rake'
165165

166-
if File.exists?('./Rakefile')
167-
load './Rakefile'
168-
end
166+
load './Rakefile' if File.exist?('./Rakefile')
169167
Rake::Task[:environment].invoke rescue nil
170-
if(!defined?(Rails))
168+
if !defined?(Rails)
171169
# Not in a Rails project, so time to load up the parts of
172170
# ActiveSupport we need.
173171
require 'active_support'

lib/annotate/annotate_routes.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# Released under the same license as Ruby. No Support. No Warranty.
1919
#
2020
module AnnotateRoutes
21-
PREFIX = "# == Route Map"
21+
PREFIX = '# == Route Map'
2222

2323
def self.do_annotations(options={})
2424
return unless(routes_exists?)
@@ -37,8 +37,8 @@ def self.do_annotations(options={})
3737
routes_map.reject!{|line| line.match(/#{options[:ignore_routes]}/)} if options[:ignore_routes]
3838

3939
header = [
40-
"#{PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime("%Y-%m-%d %H:%M")})" : ""),
41-
"#"
40+
"#{PREFIX}" + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : ''),
41+
'#'
4242
] + routes_map.map { |line| "# #{line}".rstrip }
4343

4444
existing_text = File.read(routes_file)
@@ -49,7 +49,7 @@ def self.do_annotations(options={})
4949
# middle of the file. If the number is
5050
# zero, no annotation was found.
5151

52-
if(position_after)
52+
if position_after
5353
# Ensure we have adequate trailing newlines at the end of the file to
5454
# ensure a blank line separating the content from the annotation.
5555
content << '' if(content.last != '')
@@ -89,12 +89,12 @@ def self.remove_annotations(options={})
8989
protected
9090

9191
def self.routes_file
92-
@routes_rb ||= File.join("config", "routes.rb")
92+
@routes_rb ||= File.join('config', 'routes.rb')
9393
end
9494

9595
def self.routes_exists?
9696
routes_exists = File.exists?(routes_file)
97-
puts "Can`t find routes.rb" if(!routes_exists)
97+
puts "Can't find routes.rb" if(!routes_exists)
9898
return routes_exists
9999
end
100100

@@ -105,7 +105,7 @@ def self.write_contents(existing_text, new_content)
105105

106106
return false if existing_text == new_text
107107

108-
File.open(routes_file, "wb") { |f| f.puts(new_text) }
108+
File.open(routes_file, 'wb') { |f| f.puts(new_text) }
109109
return true
110110
end
111111

@@ -117,13 +117,13 @@ def self.strip_annotations(content)
117117
content.split(/\n/, -1).each do |line|
118118
line_number += 1
119119
begin
120-
if(mode == :header)
121-
if(line !~ /\s*#/)
120+
if mode == :header
121+
if line !~ /\s*#/
122122
mode = :content
123123
raise unless (line == '')
124124
end
125-
elsif(mode == :content)
126-
if(line =~ /^\s*#\s*== Route.*$/)
125+
elsif mode == :content
126+
if line =~ /^\s*#\s*== Route.*$/
127127
header_found_at = line_number
128128
mode = :header
129129
else
@@ -147,9 +147,9 @@ def self.strip_annotations(content)
147147
end
148148

149149
def self.strip_on_removal(content, where_header_found)
150-
if(where_header_found == :before)
150+
if where_header_found == :before
151151
content.shift while(content.first == '')
152-
elsif(where_header_found == :after)
152+
elsif where_header_found == :after
153153
content.pop while(content.last == '')
154154
end
155155
# TODO: If the user buried it in the middle, we should probably see about

0 commit comments

Comments
 (0)