attributed_object 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -7
- data/lib/attributed_object.rb +0 -1
- data/lib/attributed_object/base.rb +14 -4
- data/lib/attributed_object/strict.rb +0 -1
- data/lib/attributed_object/version.rb +1 -1
- data/spec/attributed_object_spec.rb +28 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9f8eb05a90b0b2fcf0475c3c006c58b0efeaa33
|
4
|
+
data.tar.gz: 306f1a3c191fbd5b1cb19e311eea12a86b1b2f8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0707b95ee9cf2ffc1d3949086d89925d1c852a8b806779283df2753a3afc4de369077d66e4e14a6e7584b7851a0fee25bc887f2884133d8f905c610c61c7b3a
|
7
|
+
data.tar.gz: c1d84c5717b5152c18330e5fa1013e6066801dc619fbdb5faf222beac2d071bffcf5263cf6df03f8aff3d180403e4cf40fb0c93ec26d0b3cc60adb7d6dfed426
|
data/README.md
CHANGED
@@ -70,13 +70,22 @@ MyAttributedObject.new(
|
|
70
70
|
SimpleFoo.new(bar: 12) == SimpleFoo.new(bar: 12)
|
71
71
|
```
|
72
72
|
|
73
|
+
### Whitelist
|
74
|
+
```ruby
|
75
|
+
class MyAttributedObject
|
76
|
+
include AttributedObject::Strict
|
77
|
+
|
78
|
+
attribute :planet, :string, whitelist: ['Earth', 'Mars']
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
73
82
|
### Strict Type Checking
|
74
83
|
```ruby
|
75
84
|
class MyTypedAttributedObject
|
76
85
|
include AttributedObject::Strict
|
77
86
|
|
78
87
|
attribute :first, :string, disallow: nil
|
79
|
-
attribute :second, MyAttributedObject, default: nil
|
88
|
+
attribute :second, MyAttributedObject, default: nil
|
80
89
|
end
|
81
90
|
|
82
91
|
# Works
|
@@ -103,7 +112,7 @@ MyTypedAttributedObject.new(
|
|
103
112
|
)
|
104
113
|
)
|
105
114
|
|
106
|
-
# Supported Types:
|
115
|
+
# Supported Types:
|
107
116
|
# :string
|
108
117
|
# :boolean
|
109
118
|
# :integer
|
@@ -115,7 +124,7 @@ MyTypedAttributedObject.new(
|
|
115
124
|
# ArrayOf(:integer)
|
116
125
|
# HashOf(:symbol, :string)
|
117
126
|
# Instances of AttributedObject::Type (example: lib/attributed_object/types/array_of.rb)
|
118
|
-
# any Class
|
127
|
+
# any Class
|
119
128
|
```
|
120
129
|
|
121
130
|
## Coercion
|
@@ -155,7 +164,7 @@ end
|
|
155
164
|
default_to: AttributedObject::Unset, # AttributedObject::Unset | any value | AttributedObject::TypeDefaults
|
156
165
|
ignore_extra_keys: false, # false | true
|
157
166
|
coerce_blanks_to_nil: false, # false | true
|
158
|
-
disallow: AttributedObject::Unset, # AttributedObject::Unset | any value
|
167
|
+
disallow: AttributedObject::Unset, # AttributedObject::Unset | any value
|
159
168
|
}
|
160
169
|
```
|
161
170
|
|
@@ -167,7 +176,7 @@ class Defaulting
|
|
167
176
|
include AttributedObject::Strict
|
168
177
|
attributed_object default_to: nil
|
169
178
|
|
170
|
-
attribute :foo
|
179
|
+
attribute :foo
|
171
180
|
end
|
172
181
|
Defaulting.new.foo # => nil
|
173
182
|
|
@@ -206,7 +215,7 @@ class WithExtraOptions
|
|
206
215
|
include AttributedObject::Strict
|
207
216
|
attributed_object ignore_extra_keys: true
|
208
217
|
|
209
|
-
attribute :foo
|
218
|
+
attribute :foo
|
210
219
|
end
|
211
220
|
WithExtraOptions.new(foo: 'asd', something: 'bar') # this will not throw an error, usually it would
|
212
221
|
```
|
@@ -222,7 +231,7 @@ class WithExtraOptions
|
|
222
231
|
include AttributedObject::Strict
|
223
232
|
attributed_object disallow: nil
|
224
233
|
|
225
|
-
attribute :foo
|
234
|
+
attribute :foo
|
226
235
|
end
|
227
236
|
WithExtraOptions.new(foo: nil) # this will not throw an error
|
228
237
|
```
|
@@ -236,3 +245,9 @@ Result: Attributed Object is quite a bit fast (2-3x) than other gems for the spe
|
|
236
245
|
### keywords
|
237
246
|
|
238
247
|
ruby, AttributedObject, attributed object, attributed_object, value, domain driven design, ddd
|
248
|
+
|
249
|
+
# Release new Version
|
250
|
+
|
251
|
+
- bump Version Number
|
252
|
+
- `gem build attributed_object.gemspec`
|
253
|
+
- `gem push attributed_object-xxx.gem`
|
data/lib/attributed_object.rb
CHANGED
@@ -14,7 +14,8 @@ module AttributedObject
|
|
14
14
|
default_to: Unset,
|
15
15
|
ignore_extra_keys: false,
|
16
16
|
coerce_blanks_to_nil: false,
|
17
|
-
disallow: Unset
|
17
|
+
disallow: Unset,
|
18
|
+
whitelist: Unset
|
18
19
|
}.merge(parent_ops)
|
19
20
|
end
|
20
21
|
|
@@ -25,7 +26,7 @@ module AttributedObject
|
|
25
26
|
@attribute_defs = parent_defs.clone
|
26
27
|
end
|
27
28
|
|
28
|
-
def attribute(attr_name, type_info = Unset, default: Unset, disallow: Unset)
|
29
|
+
def attribute(attr_name, type_info = Unset, default: Unset, disallow: Unset, whitelist: Unset)
|
29
30
|
if default == Unset
|
30
31
|
default_to = attributed_object_options.fetch(:default_to)
|
31
32
|
|
@@ -37,13 +38,18 @@ module AttributedObject
|
|
37
38
|
if disallow == Unset
|
38
39
|
disallow = attributed_object_options.fetch(:disallow)
|
39
40
|
end
|
40
|
-
|
41
|
+
|
42
|
+
if whitelist == Unset
|
43
|
+
whitelist = attributed_object_options.fetch(:whitelist)
|
44
|
+
end
|
45
|
+
|
41
46
|
_attributed_object_check_type_supported!(type_info)
|
42
47
|
|
43
48
|
attribute_defs[attr_name] = {
|
44
49
|
type_info: type_info,
|
45
50
|
default: default,
|
46
51
|
disallow: disallow,
|
52
|
+
whitelist: whitelist
|
47
53
|
}
|
48
54
|
|
49
55
|
attr_writer attr_name
|
@@ -92,6 +98,11 @@ module AttributedObject
|
|
92
98
|
if opts[:type_info] != Unset && symbolized_args[name] != nil
|
93
99
|
symbolized_args[name] = _attributed_object_on_init_attribute(opts[:type_info], symbolized_args[name], name: name, args: args)
|
94
100
|
end
|
101
|
+
|
102
|
+
if opts[:whitelist] != Unset && !opts[:whitelist].include?(symbolized_args[name])
|
103
|
+
raise DisallowedValueError.new(self.class, name, args)
|
104
|
+
end
|
105
|
+
|
95
106
|
self.send("#{name}=", symbolized_args[name])
|
96
107
|
}
|
97
108
|
end
|
@@ -108,4 +119,3 @@ module AttributedObject
|
|
108
119
|
end
|
109
120
|
end
|
110
121
|
end
|
111
|
-
|
@@ -26,6 +26,15 @@ describe AttributedObject do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
class AttributedObjectWithWhitelist
|
30
|
+
include AttributedObject::Strict
|
31
|
+
|
32
|
+
PLANET_EARTH = 'earth'.freeze
|
33
|
+
PLANET_MARS = 'mars'.freeze
|
34
|
+
|
35
|
+
attribute :planet, :string, whitelist: [PLANET_EARTH, PLANET_MARS]
|
36
|
+
end
|
37
|
+
|
29
38
|
class ChildFoo < DefaultFoo
|
30
39
|
attribute :lollipop, default: "lecker"
|
31
40
|
end
|
@@ -45,6 +54,17 @@ describe AttributedObject do
|
|
45
54
|
end
|
46
55
|
end
|
47
56
|
|
57
|
+
describe 'whitelist' do
|
58
|
+
it 'allows whitelisted values' do
|
59
|
+
object = AttributedObjectWithWhitelist.new(planet: AttributedObjectWithWhitelist::PLANET_EARTH)
|
60
|
+
expect(object.planet).to eq(AttributedObjectWithWhitelist::PLANET_EARTH)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'crashes when provided with not-whitelisted value' do
|
64
|
+
expect { AttributedObjectWithWhitelist.new(planet: 'sun') }.to raise_error(AttributedObject::DisallowedValueError)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
48
68
|
describe 'default value' do
|
49
69
|
before { DefaultFoo.reset }
|
50
70
|
|
@@ -167,7 +187,7 @@ describe AttributedObject do
|
|
167
187
|
attribute :bar, :integer
|
168
188
|
attribute :has_other_disallow, :integer, disallow: 0
|
169
189
|
end
|
170
|
-
|
190
|
+
|
171
191
|
expect { FooWithExtra.new(bar: 1, has_other_disallow: 1) }.not_to raise_error
|
172
192
|
expect { FooWithExtra.new(bar: 1, has_other_disallow: nil) }.not_to raise_error
|
173
193
|
expect { FooWithExtra.new(bar: nil, has_other_disallow: 1) }.to raise_error(AttributedObject::DisallowedValueError)
|
@@ -203,23 +223,23 @@ describe AttributedObject do
|
|
203
223
|
expect(SimpleFoo.new(bar: 77)).to_not eq(SimpleFoo.new(bar: 12))
|
204
224
|
end
|
205
225
|
end
|
206
|
-
|
226
|
+
|
207
227
|
describe 'attribute storage' do
|
208
228
|
class InnerStructureFoo
|
209
229
|
include AttributedObject::Coerce
|
210
230
|
attribute :bar, :string, default: 'default'
|
211
231
|
attribute :foo, :string, default: 'default'
|
212
232
|
attribute :number, :integer, default: 0
|
213
|
-
|
233
|
+
|
214
234
|
def foo=(f)
|
215
235
|
@foo = "prefix-#{f}-suffix"
|
216
236
|
end
|
217
|
-
|
237
|
+
|
218
238
|
def number=(n)
|
219
239
|
@number = n+1
|
220
240
|
end
|
221
241
|
end
|
222
|
-
|
242
|
+
|
223
243
|
describe '#attributes' do
|
224
244
|
it 'returns the attributes as hash' do
|
225
245
|
expect(InnerStructureFoo.new(bar: 'hi').attributes).to eq(
|
@@ -229,15 +249,15 @@ describe AttributedObject do
|
|
229
249
|
)
|
230
250
|
end
|
231
251
|
end
|
232
|
-
|
252
|
+
|
233
253
|
it 'stores the data in instance vars' do
|
234
254
|
expect(InnerStructureFoo.new(bar: 'hi').instance_variable_get('@bar')).to eq('hi')
|
235
255
|
end
|
236
|
-
|
256
|
+
|
237
257
|
it 'uses setters' do
|
238
258
|
expect(InnerStructureFoo.new(foo: 'middel').foo).to eq('prefix-middel-suffix')
|
239
259
|
end
|
240
|
-
|
260
|
+
|
241
261
|
it 'uses setters after coercion' do
|
242
262
|
expect(InnerStructureFoo.new(number: '42').number).to eq(43)
|
243
263
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attributed_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jaap Groeneveld
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|