guideline 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/bin/guideline ADDED
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../../lib/", __FILE__)
4
+ require "guideline"
5
+ require "yaml"
6
+ require "slop"
7
+ require "active_support/all"
8
+
9
+ def parse_options
10
+ Slop.parse(:help => true) do
11
+ banner "Usage: guidline [directory] [options]"
12
+ on :c, :config, "path to guideline.yml"
13
+ end
14
+ end
15
+
16
+ def load_config(options)
17
+ path = options[:config] || File.expand_path("../../guideline.yml", __FILE__)
18
+ hash = YAML.load_file(path)
19
+ HashWithIndifferentAccess.new(hash)
20
+ end
21
+
22
+ options = parse_options
23
+ config = load_config(options)
24
+
25
+ Guideline::Visitor.new(
26
+ :only => ARGV[0],
27
+ :checker => Guideline::CheckerFactory.new(
28
+ config,
29
+ Guideline::LongLineChecker,
30
+ Guideline::LongMethodChecker,
31
+ Guideline::HardTabIndentChecker
32
+ ).create
33
+ ).check.render
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
+ require "guideline"
5
+
6
+ checker = Guideline::HardTabIndentChecker.new
7
+ visitor = Guideline::Visitor.new(:checker => checker)
8
+ visitor.check
9
+ visitor.render
data/guideline.gemspec CHANGED
@@ -17,8 +17,9 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
+ gem.add_dependency "slop"
20
21
  gem.add_dependency "active_support"
21
22
  gem.add_dependency "i18n"
22
- gem.add_development_dependency "rspec"
23
+ gem.add_development_dependency "rspec", ">=2.12.0"
23
24
  gem.add_development_dependency "pry"
24
25
  end
data/guideline.yml ADDED
@@ -0,0 +1,4 @@
1
+ "Guideline::LongLineChecker":
2
+ max: 80
3
+ "Guideline::LongMethodChecker":
4
+ max: 10
@@ -0,0 +1,18 @@
1
+ module Guideline
2
+ class CheckerFactory
3
+ def initialize(options, *checker_classes)
4
+ @options = options
5
+ @checker_classes = checker_classes
6
+ end
7
+
8
+ def create
9
+ @checker_classes.map do |klass|
10
+ klass.new(options_for(klass))
11
+ end
12
+ end
13
+
14
+ def options_for(klass)
15
+ @options[klass.to_s] || {}
16
+ end
17
+ end
18
+ end
@@ -8,7 +8,7 @@ module Guideline
8
8
  @errors ||= []
9
9
  end
10
10
 
11
- def add_error(options)
11
+ def report(options)
12
12
  errors << Error.new(
13
13
  :message => options[:message],
14
14
  :path => options[:path],
@@ -3,7 +3,7 @@ module Guideline
3
3
  def check(path)
4
4
  path.each_line.with_index do |line, index|
5
5
  if line =~ /^ *\t/
6
- add_error(
6
+ report(
7
7
  :message => "Use space indent instead of hard tab indent",
8
8
  :path => path,
9
9
  :line => index + 1
@@ -1,22 +1,20 @@
1
1
  module Guideline
2
2
  class LongLineChecker < Checker
3
3
  def check(path)
4
- lines(path).each_with_index do |line, index|
5
- if line.has_error?
6
- add_error(
7
- :message => line.message,
8
- :path => path,
9
- :line => index + 1
10
- )
11
- end
4
+ lines(path).select(&:has_error?).each do |line|
5
+ report(
6
+ :line => line.lineno,
7
+ :message => line.message,
8
+ :path => path
9
+ )
12
10
  end
13
11
  end
14
12
 
15
13
  private
16
14
 
17
15
  def lines(path)
18
- path.each_line.map do |line|
19
- LineChecker.new(line, :max => max)
16
+ path.each_line.map.with_index do |line, index|
17
+ LineChecker.new(line, :max => max, :lineno => index + 1)
20
18
  end
21
19
  end
22
20
 
@@ -47,6 +45,10 @@ module Guideline
47
45
  def length
48
46
  @line.split(//).length
49
47
  end
48
+
49
+ def lineno
50
+ @options[:lineno]
51
+ end
50
52
  end
51
53
  end
52
54
  end
@@ -6,7 +6,7 @@ module Guideline
6
6
  def check(path)
7
7
  MethodParser.parse(path.read) do |method|
8
8
  if method.height > max
9
- add_error(
9
+ report(
10
10
  :message => method.message,
11
11
  :line => method.line,
12
12
  :path => path
@@ -1,3 +1,3 @@
1
1
  module Guideline
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -12,7 +12,7 @@ module Guideline
12
12
 
13
13
  def check
14
14
  travel
15
- errors
15
+ self
16
16
  end
17
17
 
18
18
  def render
@@ -63,18 +63,28 @@ module Guideline
63
63
  private
64
64
 
65
65
  def found_paths
66
- search_paths(options[:only] || "**/*.rb")
66
+ Pathname.glob(only_pattern)
67
67
  end
68
68
 
69
69
  def excepted_paths
70
- search_paths(options[:except])
70
+ if except_pattern
71
+ Pathname.glob(only_pattern)
72
+ else
73
+ []
74
+ end
71
75
  end
72
76
 
73
- def search_paths(patterns)
74
- Array(patterns).inject([]) do |paths, pattern|
75
- paths + Pathname.glob(pattern)
77
+ def only_pattern
78
+ if options[:only]
79
+ "#{options[:only]}/**/*.rb"
80
+ else
81
+ "**/*.rb"
76
82
  end
77
83
  end
84
+
85
+ def except_pattern
86
+ options[:except]
87
+ end
78
88
  end
79
89
  end
80
90
  end
data/lib/guideline.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "guideline/version"
2
2
  require "guideline/visitor"
3
3
  require "guideline/error"
4
+ require "guideline/checker_factory"
4
5
  require "guideline/checkers/checker"
5
6
  require "guideline/checkers/long_line_checker"
6
7
  require "guideline/checkers/long_method_checker"
@@ -0,0 +1,29 @@
1
+ require "spec_helper"
2
+
3
+ module Guideline
4
+ describe CheckerFactory do
5
+ describe "#create" do
6
+ subject do
7
+ described_class.new(
8
+ {
9
+ "Guideline::LongLineChecker" => { :max => 80 },
10
+ "Guideline::LongMethodChecker" => { :max => 10 },
11
+ },
12
+ LongLineChecker,
13
+ LongMethodChecker,
14
+ ).create
15
+ end
16
+
17
+ let(:checkers) do
18
+ subject
19
+ end
20
+
21
+ it "creates instances of given classes with given config" do
22
+ LongLineChecker.should_receive(:new).with(:max => 80).and_call_original
23
+ LongMethodChecker.should_receive(:new).with(:max => 10).and_call_original
24
+ checkers[0].should be_a LongLineChecker
25
+ checkers[1].should be_a LongMethodChecker
26
+ end
27
+ end
28
+ end
29
+ end
@@ -18,21 +18,6 @@ module Guideline
18
18
  }
19
19
  end
20
20
 
21
- describe "#initialize" do
22
- subject do
23
- instance
24
- end
25
-
26
- it "saves given options" do
27
- described_class.new(options).instance_variable_get(:@options).should == options
28
- end
29
-
30
- it "can omit options" do
31
- described_class.new.instance_variable_get(:@options).should == {}
32
- end
33
- end
34
-
35
-
36
21
  describe "#errors" do
37
22
  subject do
38
23
  instance.errors
@@ -43,9 +28,9 @@ module Guideline
43
28
  end
44
29
  end
45
30
 
46
- describe "#add_error" do
31
+ describe "#report" do
47
32
  it "adds error record into its errors" do
48
- instance.add_error(error)
33
+ instance.report(error)
49
34
  instance.errors.first.message.should == "message"
50
35
  end
51
36
  end
@@ -61,7 +46,7 @@ module Guideline
61
46
 
62
47
  context "when its errors is not empty" do
63
48
  before do
64
- instance.add_error(error)
49
+ instance.report(error)
65
50
  end
66
51
 
67
52
  it { should be_true }
@@ -7,11 +7,7 @@ module Guideline
7
7
  end
8
8
 
9
9
  let(:visitor) do
10
- described_class.new(:checker => checker)
11
- end
12
-
13
- let(:options) do
14
- { :checker => checker }
10
+ described_class.new(:checker => [checker])
15
11
  end
16
12
 
17
13
  let(:checker) do
@@ -26,16 +22,6 @@ module Guideline
26
22
  mock
27
23
  end
28
24
 
29
- describe "#initialize" do
30
- subject do
31
- visitor
32
- end
33
-
34
- it "creates a new instance of #{described_class}" do
35
- be_a described_class
36
- end
37
- end
38
-
39
25
  describe "#check" do
40
26
  subject do
41
27
  visitor.check
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guideline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,8 +12,8 @@ cert_chain: []
12
12
  date: 2012-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: active_support
16
- requirement: !ruby/object:Gem::Requirement
15
+ name: slop
16
+ requirement: &70129049410700 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
24
+ version_requirements: *70129049410700
25
+ - !ruby/object:Gem::Dependency
26
+ name: active_support
27
+ requirement: &70129049409360 !ruby/object:Gem::Requirement
25
28
  none: false
26
29
  requirements:
27
30
  - - ! '>='
28
31
  - !ruby/object:Gem::Version
29
32
  version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70129049409360
30
36
  - !ruby/object:Gem::Dependency
31
37
  name: i18n
32
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &70129049423300 !ruby/object:Gem::Requirement
33
39
  none: false
34
40
  requirements:
35
41
  - - ! '>='
@@ -37,31 +43,21 @@ dependencies:
37
43
  version: '0'
38
44
  type: :runtime
39
45
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
+ version_requirements: *70129049423300
46
47
  - !ruby/object:Gem::Dependency
47
48
  name: rspec
48
- requirement: !ruby/object:Gem::Requirement
49
+ requirement: &70129049419980 !ruby/object:Gem::Requirement
49
50
  none: false
50
51
  requirements:
51
52
  - - ! '>='
52
53
  - !ruby/object:Gem::Version
53
- version: '0'
54
+ version: 2.12.0
54
55
  type: :development
55
56
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
57
+ version_requirements: *70129049419980
62
58
  - !ruby/object:Gem::Dependency
63
59
  name: pry
64
- requirement: !ruby/object:Gem::Requirement
60
+ requirement: &70129049417400 !ruby/object:Gem::Requirement
65
61
  none: false
66
62
  requirements:
67
63
  - - ! '>='
@@ -69,16 +65,12 @@ dependencies:
69
65
  version: '0'
70
66
  type: :development
71
67
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
68
+ version_requirements: *70129049417400
78
69
  description: Guideline.gem checks if your code observes your coding guidelines
79
70
  email:
80
71
  - r7kamura@gmail.com
81
- executables: []
72
+ executables:
73
+ - guideline
82
74
  extensions: []
83
75
  extra_rdoc_files: []
84
76
  files:
@@ -87,10 +79,14 @@ files:
87
79
  - LICENSE.txt
88
80
  - README.md
89
81
  - Rakefile
82
+ - bin/guideline
83
+ - examples/hard_tab_indent_checker.rb
90
84
  - examples/long_line_checker.rb
91
85
  - examples/long_method_checker.rb
92
86
  - guideline.gemspec
87
+ - guideline.yml
93
88
  - lib/guideline.rb
89
+ - lib/guideline/checker_factory.rb
94
90
  - lib/guideline/checkers/checker.rb
95
91
  - lib/guideline/checkers/hard_tab_indent_checker.rb
96
92
  - lib/guideline/checkers/long_line_checker.rb
@@ -98,6 +94,7 @@ files:
98
94
  - lib/guideline/error.rb
99
95
  - lib/guideline/version.rb
100
96
  - lib/guideline/visitor.rb
97
+ - spec/guideline/checker_factory_spec.rb
101
98
  - spec/guideline/checkers/checker_spec.rb
102
99
  - spec/guideline/checkers/hard_tab_indent_checker_spec.rb
103
100
  - spec/guideline/checkers/long_line_checker_spec.rb
@@ -125,11 +122,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
122
  version: '0'
126
123
  requirements: []
127
124
  rubyforge_project:
128
- rubygems_version: 1.8.24
125
+ rubygems_version: 1.8.15
129
126
  signing_key:
130
127
  specification_version: 3
131
128
  summary: The guideline of your code
132
129
  test_files:
130
+ - spec/guideline/checker_factory_spec.rb
133
131
  - spec/guideline/checkers/checker_spec.rb
134
132
  - spec/guideline/checkers/hard_tab_indent_checker_spec.rb
135
133
  - spec/guideline/checkers/long_line_checker_spec.rb
@@ -137,3 +135,4 @@ test_files:
137
135
  - spec/guideline/visitor_spec.rb
138
136
  - spec/guideline_spec.rb
139
137
  - spec/spec_helper.rb
138
+ has_rdoc: