cane-hashcheck 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e4a26f7dc5ae8c65a46f15a0cca16f037b33039
4
+ data.tar.gz: c65ff8d79d6d702cdab50fb4c421af67efc67507
5
+ SHA512:
6
+ metadata.gz: 374483c36079cd117b187481ab44d033e250594cb5c72cecc0b814005c52b56a092aea70803ced38a05b69ae5838c23a122acc4594c62c8c869f1153268aebce
7
+ data.tar.gz: 4093494b70d4f0c491100fb0216b1493c9e48862a8c1683fba7b5cd5161c3329323865fe675a4b11fab925d30ef8baab41166173303b6db76adafa974a414ed7
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ *.swp
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cane-hashcheck.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris Hunt
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,84 @@
1
+ # Cane Hash Check
2
+
3
+ `cane-hashcheck` enforces Ruby 1.9 hash syntax in your Ruby project using
4
+ [cane](https://github.com/square/cane).
5
+
6
+ ## Description
7
+
8
+ If you no longer like hash rockets, `cane-hashcheck` is for you. For example,
9
+ see this ugliness?
10
+
11
+ ```ruby
12
+ # person.rb
13
+ def options
14
+ {
15
+ :name => 'Bob',
16
+ :age => 30,
17
+ :location => 'Seattle'
18
+ }
19
+ end
20
+ ```
21
+
22
+ When we run run our quality rake task, we are scolded for using the old hash
23
+ syntax and encouraged to make a change on three lines:
24
+
25
+ ```bash
26
+ $ rake quality
27
+ Ruby 1.9 hash syntax violation (3):
28
+
29
+ person.rb:3
30
+ person.rb:4
31
+ person.rb:5
32
+
33
+ Total Violations: 3
34
+ ```
35
+
36
+ Ah, much better:
37
+
38
+ ```ruby
39
+ # person.rb
40
+ def options
41
+ {
42
+ name: 'Bob',
43
+ age: 30,
44
+ location: 'Seattle'
45
+ }
46
+ end
47
+ ```
48
+
49
+ ## Usage
50
+
51
+ Add `cane-hashcheck` to your project's Gemfile:
52
+
53
+ ```ruby
54
+ gem 'cane-hashcheck'
55
+ ```
56
+
57
+ Use the `Cane::HashCheck` in your quality rake task:
58
+
59
+ ```ruby
60
+ require 'cane/hashcheck'
61
+
62
+ desc 'Check code quality'
63
+ Cane::RakeTask.new(:quality) do |task|
64
+ task.use Cane::HashCheck
65
+ end
66
+ ```
67
+
68
+ Check code quality using rake:
69
+
70
+ ```bash
71
+ $ rake quality
72
+ ```
73
+
74
+ See the [cane project](https://github.com/square/cane) for general usage
75
+ instructions.
76
+
77
+ ## Contributing
78
+
79
+ 1. Fork it
80
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
81
+ 3. Commit your changes, with tests (`git commit -am 'Add some feature'`)
82
+ 4. Run the tests (`bundle exec rake`)
83
+ 5. Push to the branch (`git push origin my-new-feature`)
84
+ 6. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'cane/rake_task'
4
+ require './lib/cane/hashcheck/hash_check'
5
+
6
+ desc 'Run all tests'
7
+ RSpec::Core::RakeTask.new(:spec) do |task|
8
+ task.rspec_opts = '--color --order random'
9
+ end
10
+
11
+ desc 'Check code quality'
12
+ Cane::RakeTask.new(:quality) do |task|
13
+ task.abc_max = 9
14
+ task.use Cane::HashCheck
15
+ end
16
+
17
+ task default: :spec
18
+ task default: :quality
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cane/hashcheck/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cane-hashcheck'
8
+ spec.version = Cane::Hashcheck::VERSION
9
+ spec.authors = ['Chris Hunt']
10
+ spec.email = ['c@chrishunt.co']
11
+ spec.description = %q{Create Cane violations for pre-Ruby 1.9 hash syntax}
12
+ spec.summary = %q{Create Cane violations for pre-Ruby 1.9 hash syntax}
13
+ spec.homepage = 'https://github.com/chrishunt/cane-hashcheck'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+
25
+ spec.add_runtime_dependency 'cane'
26
+ end
@@ -0,0 +1,2 @@
1
+ require 'cane/hashcheck/version'
2
+ require 'cane/hashcheck/hash_check'
@@ -0,0 +1,59 @@
1
+ module Cane
2
+ # Creates violations for hashes using pre-Ruby 1.9 hash syntax. Hash rockets
3
+ # are allowed only if the key is a 'string' or CONSTANT.
4
+ HashCheck = Struct.new(:options) do
5
+
6
+ DESCRIPTION = 'Ruby 1.9 hash syntax violation'
7
+ DEFAULT_GLOB = '{app,lib}/**/*.rb'
8
+
9
+ def self.key; :hash; end
10
+ def self.name; 'Hash check'; end
11
+ def self.options
12
+ {
13
+ hash_glob: [
14
+ 'File glob to run hash checks over',
15
+ default: DEFAULT_GLOB,
16
+ clobber: :no_hash
17
+ ]
18
+ }
19
+ end
20
+
21
+ def violations
22
+ [].tap do |violations|
23
+ unless options[:no_hash]
24
+ each_violation do |file_name, line_number|
25
+ violations << new_violation(file_name, line_number)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def new_violation(file_name, line_number)
34
+ {
35
+ file: file_name,
36
+ line: line_number,
37
+ description: DESCRIPTION
38
+ }
39
+ end
40
+
41
+ def each_violation
42
+ file_names.each do |file_name|
43
+ ::File.read(file_name).each_line.map.with_index do |line, line_number|
44
+ if invalid? line
45
+ yield file_name, line_number + 1
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def invalid?(line)
52
+ line =~ /([^'A-Z])\s+=>\s+/
53
+ end
54
+
55
+ def file_names
56
+ Dir[ options.fetch(:hash_glob){ DEFAULT_GLOB } ]
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ module Cane
2
+ module Hashcheck
3
+ VERSION = '0.9.0'
4
+ end
5
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+ require 'tempfile'
3
+ require './lib/cane/hashcheck/hash_check'
4
+
5
+ describe Cane::HashCheck do
6
+ subject { described_class.new hash_glob: file.path }
7
+
8
+ let(:file) do
9
+ Tempfile.new('file.rb').tap do |file|
10
+ file.write file_content
11
+ file.flush
12
+ end
13
+ end
14
+
15
+ context 'when the file contains hash rockets that should not be there' do
16
+ let(:file_content) do
17
+ <<-RUBY
18
+ { totally: :valid }
19
+ { :not => 'valid' }
20
+ {
21
+ totally: 'valid',
22
+ :not => :valid,
23
+ TOTALLY => :valid
24
+ }
25
+ { 'also' => 'valid' }
26
+ RUBY
27
+ end
28
+
29
+ it 'has a violation for each hash rocket' do
30
+ expect(subject.violations.count).to eq 2
31
+ end
32
+
33
+ it 'returns the offending file names' do
34
+ subject.violations.each do |violation|
35
+ expect(violation[:file]).to eq file.path
36
+ end
37
+ end
38
+
39
+ it 'returns the offending file lines' do
40
+ expect(subject.violations[0][:line]).to eq 2
41
+ expect(subject.violations[1][:line]).to eq 5
42
+ end
43
+ end
44
+
45
+ context 'when the file has no hashrockets' do
46
+ let(:file_content) do
47
+ <<-RUBY
48
+ { totally: :valid }
49
+ { totally: 'valid' }
50
+ RUBY
51
+ end
52
+
53
+ it 'does not have violations' do
54
+ expect(subject.violations).to be_empty
55
+ end
56
+ end
57
+ end
File without changes
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cane-hashcheck
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Hunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: cane
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Create Cane violations for pre-Ruby 1.9 hash syntax
70
+ email:
71
+ - c@chrishunt.co
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - cane-hashcheck.gemspec
82
+ - lib/cane/hashcheck.rb
83
+ - lib/cane/hashcheck/hash_check.rb
84
+ - lib/cane/hashcheck/version.rb
85
+ - spec/cane/hash_check_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: https://github.com/chrishunt/cane-hashcheck
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Create Cane violations for pre-Ruby 1.9 hash syntax
111
+ test_files:
112
+ - spec/cane/hash_check_spec.rb
113
+ - spec/spec_helper.rb