kv_accessor 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +0 -0
- data/LICENSE +12 -0
- data/README.md +0 -0
- data/lib/kv_accessor/version.rb +3 -0
- data/lib/kv_accessor.rb +214 -0
- data/spec/lib/kv_accessor_spec.rb +78 -0
- data/spec/spec_helper.rb +1 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cd61aa81ee2bcd6db26543b002c1916d366a1738
|
4
|
+
data.tar.gz: 2c2095c33a8d941b8c3d5325d9555e1bf5cfc455
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 43edfb17fa1d2e34ade55e2a7b909ca6a8bfa7bcd2d788daff5c8d5f049227a85cdc72c1abea050476ea0add7b657e37ac1155653867f657479a30937fc932da
|
7
|
+
data.tar.gz: 6d2fa5e256730c6d25869499be0a30e1461b14dc0a96a0a810706912642e7d94c58dacee498e2db4b44d2dd12737eab0438431b294e26616309344acf1c8b734
|
data/CHANGELOG.md
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Copyright (c) 2016, Jeremiah McCann
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
5
|
+
|
6
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
7
|
+
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
9
|
+
|
10
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
File without changes
|
data/lib/kv_accessor.rb
ADDED
@@ -0,0 +1,214 @@
|
|
1
|
+
# Define reader and writer accessors for a Hash like instance method.
|
2
|
+
#
|
3
|
+
# Define reader and writer methods for a method returning a duck that quacks
|
4
|
+
# like a Hash (`respond_to?(:[]`). The reader/writer can be named separately
|
5
|
+
# from the ':[]' key via the alias_accessors argument (this allows for aliasing
|
6
|
+
# of complex keys).
|
7
|
+
#
|
8
|
+
# A word of warning: '#inspect' is called on the ':[]' key,
|
9
|
+
# '#to_s' is called on the 'key' name and the implimentation is via
|
10
|
+
# 'class_eval'. '#to_s' is called on the passed 'method' name and the method
|
11
|
+
# is called without regard to the receiver (eg. a Kernel method can be called
|
12
|
+
# if no instance method overrides it).
|
13
|
+
#
|
14
|
+
# This is purely meant to be a DSL for creating value style objects. Take a look
|
15
|
+
# at Struct, OpenStruct, and Virtus before choosing this.
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
# class MyCar
|
19
|
+
# attr_accessor :details
|
20
|
+
# extend KvAccessors
|
21
|
+
# kv_accessor :details, :make, year: 'model_year',
|
22
|
+
# options: { 'leather' => 'blue' }
|
23
|
+
# kv_reader 'model'
|
24
|
+
#
|
25
|
+
# def initialize(details = {})
|
26
|
+
# @details = details
|
27
|
+
# end
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# c = MyCar.new(:make => 'Chevrolet', 'model' => 'Camaro',
|
31
|
+
# 'model_year' => 1967, 'submodel' => 'SS',
|
32
|
+
# { 'leather' => 'blue' } => 2000.00)
|
33
|
+
# c.make
|
34
|
+
# #=> "Chevrolet"
|
35
|
+
# c.model
|
36
|
+
# #=> "Camaro"
|
37
|
+
# c.model = 'Corvette'
|
38
|
+
# #=> NoMethodError
|
39
|
+
# c.year
|
40
|
+
# #=> 1967
|
41
|
+
# c.year = 1968
|
42
|
+
# #=> 1968
|
43
|
+
# c.year
|
44
|
+
# #=> 1968
|
45
|
+
#
|
46
|
+
# # using a complex key with alias. The key lookup would be:
|
47
|
+
# # details[{ 'leather' => 'blue' }]
|
48
|
+
# c.options
|
49
|
+
# #=> 2000.00
|
50
|
+
# c.options
|
51
|
+
# #=> 4000.00
|
52
|
+
#
|
53
|
+
# c.submodel
|
54
|
+
# #=> NoMethodError
|
55
|
+
# c.details
|
56
|
+
# #=> { :make => "Chevrolet", "model" => "Camaro", "model_year" => 1967,
|
57
|
+
# # "submodel" => "SS", { "leather" => "blue" } => 4000.00 }
|
58
|
+
module KvAccessor
|
59
|
+
# Define reader and writter accessors for a Hash like instance method.
|
60
|
+
#
|
61
|
+
# Define reader and writter methods for each +keys+. Accessors named
|
62
|
+
# other than the key name can be defined via +aliased_accessors+.
|
63
|
+
# Calls to +name+ will call ':[]' on +method+ with either +key+ or the
|
64
|
+
# value described via +aliased_accessors+.
|
65
|
+
#
|
66
|
+
# See the overall {KvAccessors} docs for a better definition of what gets
|
67
|
+
# called on what.
|
68
|
+
#
|
69
|
+
# This is not guarded against any sort of user input. You have been warned.
|
70
|
+
#
|
71
|
+
# @example
|
72
|
+
# class MyCar
|
73
|
+
# attr_accessor :details
|
74
|
+
# extend KvAccessors
|
75
|
+
# kv_accessor :details, :make, 'model', year: :model_year
|
76
|
+
#
|
77
|
+
# def initialize(details = {})
|
78
|
+
# @details = details
|
79
|
+
# end
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# c = MyCar.new(:make => 'Chevrolet', 'model' => 'Camaro',
|
83
|
+
# :model_year => 1967, 'submodel' => 'SS')
|
84
|
+
# c.make
|
85
|
+
# #=> "Chevrolet"
|
86
|
+
# c.model
|
87
|
+
# #=> "Camaro"
|
88
|
+
# c.year
|
89
|
+
# #=> 1966
|
90
|
+
# c.year = 1967
|
91
|
+
# #=> 1967
|
92
|
+
# c.year
|
93
|
+
# #=> 1967
|
94
|
+
# c.submodel
|
95
|
+
# #=> NoMethodError
|
96
|
+
# c.details
|
97
|
+
# #=> { :make => "Chevrolet", "model" => "Camaro", :model_year => 1968,
|
98
|
+
# # "submodel" => "SS"
|
99
|
+
def kv_accessor(method, *keys, **aliased_accessors)
|
100
|
+
kv_reader(method, *keys, **aliased_accessors)
|
101
|
+
kv_writer(method, *keys, **aliased_accessors)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Define reader accessors for a Hash like instance method.
|
105
|
+
#
|
106
|
+
# Define reader accessor methods for each +keys+. Accessors named other than
|
107
|
+
# the key name can be defined via +aliased_accessors+. Calls to +name+ will
|
108
|
+
# call ':[]' on +method+ with either +key+ or the value described via
|
109
|
+
# +aliased_accessors+.
|
110
|
+
#
|
111
|
+
# See the overall {KvAccessors} docs for a better definition of what gets
|
112
|
+
# called on what.
|
113
|
+
#
|
114
|
+
# This is not guarded against any sort of user input. You have been warned.
|
115
|
+
#
|
116
|
+
# @example
|
117
|
+
# class MyCar
|
118
|
+
# attr_accessor :details
|
119
|
+
# extend KvAccessors
|
120
|
+
# kv_accessor :details, :make, 'model', year: :model_year
|
121
|
+
#
|
122
|
+
# def initialize(details = {})
|
123
|
+
# @details = details
|
124
|
+
# end
|
125
|
+
# end
|
126
|
+
#
|
127
|
+
# c = MyCar.new(:make => 'Chevrolet', 'model' => 'Camaro',
|
128
|
+
# :model_year => 1967, 'submodel' => 'SS')
|
129
|
+
# c.make #=> "Chevrolet"
|
130
|
+
# c.model #=> "Camaro"
|
131
|
+
# c.year #=> 1967
|
132
|
+
# c.year = 1968
|
133
|
+
# c.year #=> 1968
|
134
|
+
# c.submodel #=> NoMethodError
|
135
|
+
# c.details
|
136
|
+
# #=> { :make => "Chevrolet", "model" => "Camaro", :model_year => 1968,
|
137
|
+
# "submodel" => "SS" }
|
138
|
+
def kv_reader(method, *keys, **aliased_accessors)
|
139
|
+
accessors = Hash[keys.map { |v| [v, v] }].merge(aliased_accessors)
|
140
|
+
accessors.each do |name, key|
|
141
|
+
begin
|
142
|
+
line_no = __LINE__ + 1
|
143
|
+
str = <<-EOMETHODDEF
|
144
|
+
def #{name}
|
145
|
+
#{method}[#{key.inspect}]
|
146
|
+
end
|
147
|
+
EOMETHODDEF
|
148
|
+
|
149
|
+
module_eval(str, __FILE__, line_no)
|
150
|
+
# If it's not a class or module, it's an instance
|
151
|
+
rescue NoMethodError
|
152
|
+
instance_eval(str, __FILE__, line_no)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# Define writter accessors for a Hash like instance method.
|
158
|
+
#
|
159
|
+
# Define writter accessor methods for each +keys+. Accessors named other than
|
160
|
+
# the key name can be defined via +aliased_accessors+. Calls to +name+ will
|
161
|
+
# call ':[]' on +method+ with either +key+ or the value described via
|
162
|
+
# +aliased_accessors+.
|
163
|
+
#
|
164
|
+
# See the overall {KvAccessors} docs for a better definition of what gets
|
165
|
+
# called on what.
|
166
|
+
#
|
167
|
+
# This is not guarded against any sort of user input. You have been warned.
|
168
|
+
#
|
169
|
+
# @example
|
170
|
+
# class MyCar
|
171
|
+
# attr_accessor :details
|
172
|
+
# extend KvAccessors
|
173
|
+
# kv_writer :details, 'model', year: :model_year
|
174
|
+
#
|
175
|
+
# def initialize(details = {})
|
176
|
+
# @details = details
|
177
|
+
# end
|
178
|
+
# end
|
179
|
+
#
|
180
|
+
# c = MyCar.new(:make => 'Chevrolet', 'model' => 'Camaro',
|
181
|
+
# :model_year => 1967, 'submodel' => 'SS')
|
182
|
+
# c.make
|
183
|
+
# #=> NoMethodError
|
184
|
+
# c.model = 'Corvette'
|
185
|
+
# #=> "Corevette"
|
186
|
+
# #=> 1967
|
187
|
+
# c.year = 1968
|
188
|
+
# #=> 1968
|
189
|
+
# c.year
|
190
|
+
# #=> NoMethodError
|
191
|
+
# c.submodel = 'RS'
|
192
|
+
# #=> NoMethodError
|
193
|
+
# c.details
|
194
|
+
# #=> { :make => "Chevrolet", "model" => "Corvette", :model_year => 1968,
|
195
|
+
# # "submodel" => "SS"
|
196
|
+
def kv_writer(method, *keys, **aliased_accessors)
|
197
|
+
accessors = Hash[keys.map { |v| [v, v] }].merge(aliased_accessors)
|
198
|
+
accessors.each do |name, key|
|
199
|
+
begin
|
200
|
+
line_no = __LINE__ + 1
|
201
|
+
str = <<-EOMETHODDEF
|
202
|
+
def #{name}=(value)
|
203
|
+
#{method}[#{key.inspect}] = value
|
204
|
+
end
|
205
|
+
EOMETHODDEF
|
206
|
+
|
207
|
+
module_eval(str, __FILE__, line_no)
|
208
|
+
# If it's not a class or module, it's an instance
|
209
|
+
rescue NoMethodError
|
210
|
+
instance_eval(str, __FILE__, line_no)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe KvAccessor do
|
4
|
+
let(:default_details) do
|
5
|
+
{
|
6
|
+
:make => 'Chevrolet', 'model' => 'Camaro', :model => 'Key not string',
|
7
|
+
'submodel' => 'SS', 'model_year' => 1967, price: 40_000.00,
|
8
|
+
{ 'leather' => 'blue' } => 2_000.00
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:default_other) { { upc: '808' } }
|
13
|
+
|
14
|
+
let(:kv_class) do
|
15
|
+
Class.new do
|
16
|
+
attr_accessor :details, :other
|
17
|
+
extend KvAccessor
|
18
|
+
|
19
|
+
kv_accessor :details, :make, year: 'model_year',
|
20
|
+
blue_interior: { 'leather' => 'blue' }
|
21
|
+
kv_reader :details, 'model', :price
|
22
|
+
kv_writer :details, 'color', :price
|
23
|
+
|
24
|
+
kv_accessor :other, :upc
|
25
|
+
|
26
|
+
def initialize(details, other)
|
27
|
+
self.details = details
|
28
|
+
self.other = other
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# For the expect to change, the duping is somehow necessary
|
34
|
+
subject(:data_object) { kv_class.new(default_details.dup, default_other.dup) }
|
35
|
+
|
36
|
+
it 'responds to all the accessor names' do
|
37
|
+
expect(data_object).to respond_to(:make, :year, :blue_interior, :make=,
|
38
|
+
:year=, :blue_interior=, :model, :price,
|
39
|
+
:color=, :price=, :upc)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'does not respond to unspecified accessor names' do
|
43
|
+
expect(data_object).not_to respond_to(:submodel, :model=)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'has all of the corresponding reader attributes' do
|
47
|
+
expect(data_object).to have_attributes(
|
48
|
+
make: 'Chevrolet', model: 'Camaro', blue_interior: 2_000.00,
|
49
|
+
price: 40_000.00, year: 1967, upc: '808'
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'updates only the explicit keys on attribute assignment' do
|
54
|
+
# There's some funky stuff going on with the ordering between `from()`,
|
55
|
+
# `change { }`, and `expect { }`. `change { }.from` doesn't seem to compare
|
56
|
+
# the objects first, before running the expect statement. I think it only
|
57
|
+
# saves the initial output, runs the expect statement, runs the change block
|
58
|
+
# again, and then finally compares the results. So any order dependent
|
59
|
+
# results need to be another copy.
|
60
|
+
expect {
|
61
|
+
data_object.make = 'Ford'
|
62
|
+
data_object.price = 41_000.00
|
63
|
+
data_object.year = 1968
|
64
|
+
data_object.blue_interior = 3_000.00
|
65
|
+
data_object.color = 'red'
|
66
|
+
data_object.upc = '809'
|
67
|
+
}.to change { data_object.details }.from(
|
68
|
+
default_details
|
69
|
+
).to(
|
70
|
+
default_details.merge(
|
71
|
+
:make => 'Ford', :price => 41_000.00, 'model_year' => 1968,
|
72
|
+
'color' => 'red', { 'leather' => 'blue' } => 3_000.00,
|
73
|
+
)
|
74
|
+
).and change { data_object.other }.from(
|
75
|
+
default_other
|
76
|
+
).to(upc: '809')
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'kv_accessor'
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kv_accessor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremiah McCann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.4'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.4'
|
27
|
+
description: Pretty much Forwardable for key-value objects
|
28
|
+
email:
|
29
|
+
- kv.accessor.gem@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- CHANGELOG.md
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- lib/kv_accessor.rb
|
38
|
+
- lib/kv_accessor/version.rb
|
39
|
+
- spec/lib/kv_accessor_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
homepage: http://github.com/jmhmccr/kv_accessor
|
42
|
+
licenses:
|
43
|
+
- BSD-3-Clause
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib/
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.5.1
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Generate accessor methods for an indexed object
|
65
|
+
test_files:
|
66
|
+
- spec/lib/kv_accessor_spec.rb
|
67
|
+
- spec/spec_helper.rb
|