guideline 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in guideline.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ryo NAKAMURA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # Guideline
2
+
3
+ ## Installation
4
+
5
+ ```
6
+ $ gem install guideline
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ See https://github.com/r7kamura/guideline/tree/master/examples
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
+ require "guideline"
5
+
6
+ checker = Guideline::LongLineChecker.new(:max => 128)
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
data/guideline.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'guideline/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "guideline"
8
+ gem.version = Guideline::VERSION
9
+ gem.authors = ["Ryo NAKAMURA"]
10
+ gem.email = ["r7kamura@gmail.com"]
11
+ gem.description = "Guideline.gem checks if your code observes your coding guidelines"
12
+ gem.summary = "The guideline of your code"
13
+ gem.homepage = "https://github.com/r7kamura/guideline"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "active_support"
21
+ gem.add_dependency "i18n"
22
+ gem.add_development_dependency "rspec"
23
+ gem.add_development_dependency "pry"
24
+ end
@@ -0,0 +1,52 @@
1
+ module Guideline
2
+ class LongLineChecker < Checker
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
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def lines(path)
18
+ path.each_line.map do |line|
19
+ LineChecker.new(line, :max => max)
20
+ end
21
+ end
22
+
23
+ def max
24
+ @options[:max]
25
+ end
26
+
27
+ class LineChecker
28
+ def initialize(line, options = {})
29
+ @line = line
30
+ @options = options
31
+ end
32
+
33
+ def message
34
+ actual = "%3d" % length
35
+ limit = "%3d" % max
36
+ "Line length #{actual} should be less than #{limit} characters"
37
+ end
38
+
39
+ def has_error?
40
+ length > max
41
+ end
42
+
43
+ def max
44
+ @options[:max] || 80
45
+ end
46
+
47
+ def length
48
+ @line.split(//).length
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ module Guideline
2
+ class Checker
3
+ def initialize(options = {})
4
+ @options = options
5
+ end
6
+
7
+ def errors
8
+ @errors ||= []
9
+ end
10
+
11
+ def add_error(options)
12
+ errors << OpenStruct.new(
13
+ :message => options[:message],
14
+ :path => options[:path],
15
+ :line => options[:line]
16
+ )
17
+ end
18
+
19
+ def has_error?
20
+ !errors.empty?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Guideline
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,71 @@
1
+ require "pathname"
2
+
3
+ module Guideline
4
+ class Visitor
5
+ attr_reader :options
6
+
7
+ def initialize(options, &block)
8
+ @options = options
9
+ @block = block
10
+ end
11
+
12
+ def check
13
+ travel
14
+ errors
15
+ end
16
+
17
+ private
18
+
19
+ def travel
20
+ paths.each do |path|
21
+ checkers.each do |checker|
22
+ checker.check(path)
23
+ end
24
+ end
25
+ end
26
+
27
+ def paths
28
+ PathFinder.find(options)
29
+ end
30
+
31
+ def checkers
32
+ @checkers ||= Array(options[:checker])
33
+ end
34
+
35
+ def errors
36
+ @errors ||= checkers.select(&:has_error?).map(&:errors).inject([], &:+)
37
+ end
38
+
39
+ class PathFinder
40
+ attr_reader :options
41
+
42
+ def self.find(options = {})
43
+ new(options).paths
44
+ end
45
+
46
+ def initialize(options = {})
47
+ @options = options
48
+ end
49
+
50
+ def paths
51
+ (found_paths - excepted_paths).select(&:exist?)
52
+ end
53
+
54
+ private
55
+
56
+ def found_paths
57
+ search_paths(options[:only] || "**/*.rb")
58
+ end
59
+
60
+ def excepted_paths
61
+ search_paths(options[:except])
62
+ end
63
+
64
+ def search_paths(patterns)
65
+ Array(patterns).inject([]) do |paths, pattern|
66
+ paths + Pathname.glob(pattern)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
data/lib/guideline.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "ostruct"
2
+ require "active_support/all"
3
+
4
+ require "guideline/version"
5
+ require "guideline/visitor"
6
+ require "guideline/checker"
7
+ require "guideline/checker/long_line_checker"
@@ -0,0 +1,71 @@
1
+ require "spec_helper"
2
+
3
+ module Guideline
4
+ describe Checker do
5
+ let(:instance) do
6
+ described_class.new(options)
7
+ end
8
+
9
+ let(:options) do
10
+ mock
11
+ end
12
+
13
+ let(:error) do
14
+ {
15
+ :message => "message",
16
+ :path => "path",
17
+ :line => "line",
18
+ }
19
+ end
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
+ describe "#errors" do
37
+ subject do
38
+ instance.errors
39
+ end
40
+
41
+ it "returns an Array" do
42
+ should be_a Array
43
+ end
44
+ end
45
+
46
+ describe "#add_error" do
47
+ it "adds error record into its errors" do
48
+ instance.add_error(error)
49
+ instance.errors.first.message.should == "message"
50
+ end
51
+ end
52
+
53
+ describe "#has_error?" do
54
+ subject do
55
+ instance.has_error?
56
+ end
57
+
58
+ context "when its errors is empty" do
59
+ it { should be_false }
60
+ end
61
+
62
+ context "when its errors is not empty" do
63
+ before do
64
+ instance.add_error(error)
65
+ end
66
+
67
+ it { should be_true }
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ module Guideline
4
+ describe Visitor do
5
+ before do
6
+ visitor.stub(:paths).and_return(paths)
7
+ end
8
+
9
+ let(:visitor) do
10
+ described_class.new(:checker => checker)
11
+ end
12
+
13
+ let(:options) do
14
+ { :checker => checker }
15
+ end
16
+
17
+ let(:checker) do
18
+ mock(:has_error? => false)
19
+ end
20
+
21
+ let(:paths) do
22
+ [path]
23
+ end
24
+
25
+ let(:path) do
26
+ mock
27
+ end
28
+
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
+ describe "#check" do
40
+ subject do
41
+ visitor.check
42
+ end
43
+
44
+ it "calls checker#check with matched path" do
45
+ checker.should_receive(:check).with(path)
46
+ subject
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ require "spec_helper"
2
+
3
+ module Guideline
4
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "guideline"
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guideline
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryo NAKAMURA
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active_support
16
+ requirement: &70150348023000 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70150348023000
25
+ - !ruby/object:Gem::Dependency
26
+ name: i18n
27
+ requirement: &70150348021800 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70150348021800
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70150348020440 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70150348020440
47
+ - !ruby/object:Gem::Dependency
48
+ name: pry
49
+ requirement: &70150348034340 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70150348034340
58
+ description: Guideline.gem checks if your code observes your coding guidelines
59
+ email:
60
+ - r7kamura@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - examples/long_line_checker.rb
71
+ - guideline.gemspec
72
+ - lib/guideline.rb
73
+ - lib/guideline/checker.rb
74
+ - lib/guideline/checker/long_line_checker.rb
75
+ - lib/guideline/version.rb
76
+ - lib/guideline/visitor.rb
77
+ - spec/guideline/checker_spec.rb
78
+ - spec/guideline/visitor_spec.rb
79
+ - spec/guideline_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/r7kamura/guideline
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.15
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: The guideline of your code
105
+ test_files:
106
+ - spec/guideline/checker_spec.rb
107
+ - spec/guideline/visitor_spec.rb
108
+ - spec/guideline_spec.rb
109
+ - spec/spec_helper.rb
110
+ has_rdoc: