madhattr 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/Guardfile +28 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +7 -0
- data/lib/madhattr/hattr_accessor.rb +31 -0
- data/lib/madhattr/version.rb +3 -0
- data/lib/madhattr.rb +6 -0
- data/madhattr.gemspec +27 -0
- data/spec/hattr_accessor_spec.rb +97 -0
- data/spec/sample_object.rb +13 -0
- data/spec/spec_helper.rb +8 -0
- metadata +159 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :minitest do
|
5
|
+
# with Minitest::Unit
|
6
|
+
watch(%r{^test/(.*)\/?test_(.*)\.rb})
|
7
|
+
watch(%r{^lib/(.*/)?([^/]+)\.rb}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
|
8
|
+
watch(%r{^test/test_helper\.rb}) { 'test' }
|
9
|
+
|
10
|
+
# with Minitest::Spec
|
11
|
+
# watch(%r{^spec/(.*)_spec\.rb})
|
12
|
+
# watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
|
13
|
+
# watch(%r{^spec/spec_helper\.rb}) { 'spec' }
|
14
|
+
|
15
|
+
# Rails 4
|
16
|
+
# watch(%r{^app/(.+)\.rb}) { |m| "test/#{m[1]}_test.rb" }
|
17
|
+
# watch(%r{^app/controllers/application_controller\.rb}) { 'test/controllers' }
|
18
|
+
# watch(%r{^app/controllers/(.+)_controller\.rb}) { |m| "test/integration/#{m[1]}_test.rb" }
|
19
|
+
# watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" }
|
20
|
+
# watch(%r{^lib/(.+)\.rb}) { |m| "test/lib/#{m[1]}_test.rb" }
|
21
|
+
# watch(%r{^test/.+_test\.rb})
|
22
|
+
# watch(%r{^test/test_helper\.rb}) { 'test' }
|
23
|
+
|
24
|
+
# Rails < 4
|
25
|
+
# watch(%r{^app/controllers/(.*)\.rb}) { |m| "test/functional/#{m[1]}_test.rb" }
|
26
|
+
# watch(%r{^app/helpers/(.*)\.rb}) { |m| "test/helpers/#{m[1]}_test.rb" }
|
27
|
+
# watch(%r{^app/models/(.*)\.rb}) { |m| "test/unit/#{m[1]}_test.rb" }
|
28
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jacob Stetser
|
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,51 @@
|
|
1
|
+
# Madhattr
|
2
|
+
|
3
|
+
[](http://allthebadges.io/wideopenspaces/madhattr/badge_fury)
|
4
|
+
[](http://allthebadges.io/wideopenspaces/madhattr/gemnasium)
|
5
|
+
[](http://allthebadges.io/wideopenspaces/madhattr/travis)
|
6
|
+
[](http://allthebadges.io/wideopenspaces/madhattr/coveralls)
|
7
|
+
[](http://allthebadges.io/wideopenspaces/madhattr/code_climate)
|
8
|
+
|
9
|
+
Basic accessors (readers/writers) for hash elements, such as options hashes!
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'madhattr', require: false
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install madhattr
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
It's easy: when you intend to use it
|
28
|
+
|
29
|
+
require 'madhattr/hattr_accessor'
|
30
|
+
|
31
|
+
and then, in your class:
|
32
|
+
|
33
|
+
extend HattrAccessor
|
34
|
+
|
35
|
+
hattr_reader :source_hash, *keys_to_be_accessorized
|
36
|
+
|
37
|
+
e.g.,
|
38
|
+
|
39
|
+
hattr_reader :animals, :cat, :bird, :dog, :snake
|
40
|
+
|
41
|
+
You can also create writers using `hattr_writer` or both readers and writers using `hattr_accessor`.
|
42
|
+
|
43
|
+
Enjoy!
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module HattrAccessor
|
2
|
+
def hattr_reader(source, *keys)
|
3
|
+
ensure_readable source
|
4
|
+
keys.each do |key|
|
5
|
+
define_method(key) { send(source).fetch(key) }
|
6
|
+
end
|
7
|
+
end
|
8
|
+
alias_method :hattr_readers, :hattr_reader
|
9
|
+
|
10
|
+
def hattr_writer(source, *keys)
|
11
|
+
ensure_readable source
|
12
|
+
keys.each do |key|
|
13
|
+
define_method("#{key}=") do |value|
|
14
|
+
send("#{source}").store(key, value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
alias_method :hattr_writers, :hattr_writer
|
19
|
+
|
20
|
+
def hattr_accessor(source, *keys)
|
21
|
+
hattr_reader source, *keys
|
22
|
+
hattr_writer source, *keys
|
23
|
+
end
|
24
|
+
alias_method :hattr_accessors, :hattr_accessor
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
def ensure_readable(source)
|
29
|
+
attr_reader(source) unless method_defined?(source)
|
30
|
+
end
|
31
|
+
end
|
data/lib/madhattr.rb
ADDED
data/madhattr.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'madhattr/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "madhattr"
|
8
|
+
spec.version = Madhattr::VERSION
|
9
|
+
spec.authors = ["Jacob Stetser"]
|
10
|
+
spec.email = ["jake@wideopenspac.es"]
|
11
|
+
spec.description = %q{Add accessors for a hash}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = 'https://github.com/wideopenspaces/Madhattr'
|
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 'guard'
|
24
|
+
spec.add_development_dependency 'guard-minitest'
|
25
|
+
spec.add_development_dependency 'minitest', '~> 5.0.0'
|
26
|
+
spec.add_development_dependency 'minitest-spec-context'
|
27
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'sample_object'
|
3
|
+
|
4
|
+
describe HattrAccessor do
|
5
|
+
context 'when the source object exists' do
|
6
|
+
subject { SampleObject.new }
|
7
|
+
|
8
|
+
context 'cat' do
|
9
|
+
it 'has a reader' do
|
10
|
+
subject.must_respond_to :cat
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'does not have a writer' do
|
14
|
+
subject.wont_respond_to :cat=
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'returns the correct value when accessed' do
|
18
|
+
subject.cat.must_equal(1)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'dog' do
|
23
|
+
it 'does not have a reader' do
|
24
|
+
subject.wont_respond_to :dog
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'has a writer' do
|
28
|
+
subject.must_respond_to :dog=
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'sets the correct value' do
|
32
|
+
subject.dog = 5
|
33
|
+
subject.options[:dog].must_equal 5
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'bird' do
|
38
|
+
it 'has a reader' do
|
39
|
+
subject.must_respond_to :bird
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'has a writer' do
|
43
|
+
subject.must_respond_to :bird=
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'returns the correct value when accessed' do
|
47
|
+
subject.bird.must_equal('wut')
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'sets the correct value' do
|
51
|
+
subject.bird = 'lol'
|
52
|
+
subject.bird.must_equal('lol')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'snake' do
|
57
|
+
it 'has a reader' do
|
58
|
+
subject.must_respond_to :snake
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'has a writer' do
|
62
|
+
subject.must_respond_to :snake=
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'returns the correct value when accessed' do
|
66
|
+
subject.snake.must_equal(:no)
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'sets the correct value' do
|
70
|
+
subject.snake = 0
|
71
|
+
subject.snake.must_equal(0)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'dinosaur' do
|
76
|
+
it 'does not have a reader' do
|
77
|
+
subject.wont_respond_to :dinosaur
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'does not have a writer' do
|
81
|
+
subject.wont_respond_to :dinosaur=
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'a key that does not exist' do
|
86
|
+
context 'reader' do
|
87
|
+
it 'exists' do
|
88
|
+
subject.must_respond_to :aardvark
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'raises an error when accessed' do
|
92
|
+
lambda { subject.aardvark }.must_raise(KeyError)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'madhattr'
|
2
|
+
|
3
|
+
class SampleObject
|
4
|
+
extend HattrAccessor
|
5
|
+
|
6
|
+
hattr_reader :options, :cat, :snake
|
7
|
+
hattr_writer :options, :dog, :snake
|
8
|
+
hattr_accessor :options, :bird, :aardvark
|
9
|
+
|
10
|
+
def initialize(options = {cat: 1, dog: 2, bird: 'wut', snake: :no, dinosaur: 'extinct'})
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: madhattr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jacob Stetser
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-11-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: guard
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: guard-minitest
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: minitest
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 5.0.0
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 5.0.0
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: minitest-spec-context
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Add accessors for a hash
|
111
|
+
email:
|
112
|
+
- jake@wideopenspac.es
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- .gitignore
|
118
|
+
- .travis.yml
|
119
|
+
- Gemfile
|
120
|
+
- Guardfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- lib/madhattr.rb
|
125
|
+
- lib/madhattr/hattr_accessor.rb
|
126
|
+
- lib/madhattr/version.rb
|
127
|
+
- madhattr.gemspec
|
128
|
+
- spec/hattr_accessor_spec.rb
|
129
|
+
- spec/sample_object.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
homepage: https://github.com/wideopenspaces/Madhattr
|
132
|
+
licenses:
|
133
|
+
- MIT
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
140
|
+
requirements:
|
141
|
+
- - ! '>='
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ! '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 1.8.23
|
153
|
+
signing_key:
|
154
|
+
specification_version: 3
|
155
|
+
summary: Add accessors for a hash
|
156
|
+
test_files:
|
157
|
+
- spec/hattr_accessor_spec.rb
|
158
|
+
- spec/sample_object.rb
|
159
|
+
- spec/spec_helper.rb
|