guideline 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Guideline
2
+ Check your code
2
3
 
3
4
  ## Installation
4
5
 
@@ -7,5 +8,18 @@ $ gem install guideline
7
8
  ```
8
9
 
9
10
  ## Usage
11
+ https://github.com/r7kamura/guideline/tree/master/examples
10
12
 
11
- See https://github.com/r7kamura/guideline/tree/master/examples
13
+ ```
14
+ $ ruby examples/long_line_checker.rb
15
+ spec/guideline/checker_spec.rb
16
+ 27: Line length 88 should be less than 80 characters
17
+
18
+ $ ruby examples/long_method_checker.rb
19
+ lib/guideline/checker/long_line_checker.rb
20
+ 3: Too long 9 lines method <#check>
21
+
22
+ lib/guideline/checker/long_method_checker.rb
23
+ 6: Too long 9 lines method <#check>
24
+ 42: Too long 10 lines method <#on_def>
25
+ ```
@@ -3,27 +3,7 @@
3
3
  $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
4
  require "guideline"
5
5
 
6
- checker = Guideline::LongLineChecker.new(:max => 128)
6
+ checker = Guideline::LongLineChecker.new(:max => 80)
7
7
  visitor = Guideline::Visitor.new(:checker => checker)
8
- errors = visitor.check
9
- errors.group_by(&:path).each do |path, error_set|
10
- puts path
11
- error_set.each do |error|
12
- puts "%4d: %s" % [error.line, error.message]
13
- end
14
- puts
15
- end
16
-
17
- __END__
18
- app/controllers/blogs_controller.rb
19
- 8: Line length 169 should be less than 128 characters
20
-
21
- app/models/blog.rb
22
- 4: Line length 169 should be less than 128 characters
23
-
24
- spec/controllers/blogs_controller_spec.rb
25
- 6: Line length 139 should be less than 128 characters
26
- 10: Line length 194 should be less than 128 characters
27
- 16: Line length 145 should be less than 128 characters
28
- 17: Line length 139 should be less than 128 characters
29
- 21: Line length 194 should be less than 128 characters
8
+ visitor.check
9
+ visitor.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::LongMethodChecker.new(:max => 5)
7
+ visitor = Guideline::Visitor.new(:checker => checker)
8
+ visitor.check
9
+ visitor.render
@@ -0,0 +1,73 @@
1
+ require "ripper"
2
+ require "ostruct"
3
+
4
+ module Guideline
5
+ class LongMethodChecker < Checker
6
+ def check(path)
7
+ MethodParser.parse(path.read) do |method|
8
+ if method.height > max
9
+ add_error(
10
+ :message => method.message,
11
+ :line => method.line,
12
+ :path => path
13
+ )
14
+ end
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def max
21
+ @options[:max] || 50
22
+ end
23
+
24
+ class Method < OpenStruct
25
+ def message
26
+ "Too long %3d lines method <#%s>" % [height, name]
27
+ end
28
+ end
29
+
30
+ class MethodParser < Ripper
31
+ class << self
32
+ attr_reader :callback
33
+
34
+ def parse(*, &callback)
35
+ @callback = callback
36
+ super
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def on_def(name, *)
43
+ start = lineno_stack.pop
44
+ finish = lineno
45
+ height = finish - start - 1
46
+ method = Method.new(
47
+ :name => name,
48
+ :line => start,
49
+ :height => height
50
+ )
51
+ call(method)
52
+ super
53
+ end
54
+
55
+ def on_kw(name)
56
+ lineno_stack << lineno if name == "def"
57
+ super
58
+ end
59
+
60
+ def lineno_stack
61
+ @lineno_stack ||= []
62
+ end
63
+
64
+ def call(method)
65
+ callback.call(method)
66
+ end
67
+
68
+ def callback
69
+ self.class.callback
70
+ end
71
+ end
72
+ end
73
+ end
@@ -9,7 +9,7 @@ module Guideline
9
9
  end
10
10
 
11
11
  def add_error(options)
12
- errors << OpenStruct.new(
12
+ errors << Error.new(
13
13
  :message => options[:message],
14
14
  :path => options[:path],
15
15
  :line => options[:line]
@@ -0,0 +1,9 @@
1
+ require "ostruct"
2
+
3
+ module Guideline
4
+ class Error < OpenStruct
5
+ def render
6
+ puts "%4d: %s" % [line, message]
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Guideline
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require "pathname"
2
+ require "active_support/all"
2
3
 
3
4
  module Guideline
4
5
  class Visitor
@@ -14,6 +15,14 @@ module Guideline
14
15
  errors
15
16
  end
16
17
 
18
+ def render
19
+ errors.group_by(&:path).each do |path, errors|
20
+ puts path
21
+ errors.each(&:render)
22
+ puts
23
+ end
24
+ end
25
+
17
26
  private
18
27
 
19
28
  def travel
data/lib/guideline.rb CHANGED
@@ -1,7 +1,6 @@
1
- require "ostruct"
2
- require "active_support/all"
3
-
4
1
  require "guideline/version"
5
2
  require "guideline/visitor"
3
+ require "guideline/error"
6
4
  require "guideline/checker"
7
5
  require "guideline/checker/long_line_checker"
6
+ require "guideline/checker/long_method_checker"
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ module Guideline
4
+ describe LongLineChecker do
5
+ describe "#check" do
6
+ subject do
7
+ checker.check(path)
8
+ end
9
+
10
+ let(:checker) do
11
+ described_class.new(:max => 1)
12
+ end
13
+
14
+ let(:path) do
15
+ Pathname.new(__FILE__)
16
+ end
17
+
18
+ it do
19
+ should be_a Array
20
+ end
21
+
22
+ it do
23
+ subject
24
+ checker.should have_error
25
+ end
26
+ end
27
+ end
28
+ end
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-20 00:00:00.000000000 Z
12
+ date: 2012-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_support
16
- requirement: &70150348023000 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70150348023000
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: i18n
27
- requirement: &70150348021800 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70150348021800
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rspec
38
- requirement: &70150348020440 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70150348020440
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: pry
49
- requirement: &70150348034340 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70150348034340
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  description: Guideline.gem checks if your code observes your coding guidelines
59
79
  email:
60
80
  - r7kamura@gmail.com
@@ -68,12 +88,16 @@ files:
68
88
  - README.md
69
89
  - Rakefile
70
90
  - examples/long_line_checker.rb
91
+ - examples/long_method_checker.rb
71
92
  - guideline.gemspec
72
93
  - lib/guideline.rb
73
94
  - lib/guideline/checker.rb
74
95
  - lib/guideline/checker/long_line_checker.rb
96
+ - lib/guideline/checker/long_method_checker.rb
97
+ - lib/guideline/error.rb
75
98
  - lib/guideline/version.rb
76
99
  - lib/guideline/visitor.rb
100
+ - spec/guideline/checker/long_line_checker_spec.rb
77
101
  - spec/guideline/checker_spec.rb
78
102
  - spec/guideline/visitor_spec.rb
79
103
  - spec/guideline_spec.rb
@@ -98,13 +122,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
122
  version: '0'
99
123
  requirements: []
100
124
  rubyforge_project:
101
- rubygems_version: 1.8.15
125
+ rubygems_version: 1.8.24
102
126
  signing_key:
103
127
  specification_version: 3
104
128
  summary: The guideline of your code
105
129
  test_files:
130
+ - spec/guideline/checker/long_line_checker_spec.rb
106
131
  - spec/guideline/checker_spec.rb
107
132
  - spec/guideline/visitor_spec.rb
108
133
  - spec/guideline_spec.rb
109
134
  - spec/spec_helper.rb
110
- has_rdoc: