attributed_object 0.2.2 → 0.3
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 +4 -4
- data/.gitignore +2 -0
- data/README.md +16 -0
- data/lib/attributed_object/base.rb +6 -1
- data/lib/attributed_object/version.rb +1 -1
- data/spec/attributed_object_spec.rb +16 -0
- 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: 93bb8f83e8a3c78c45869a3b99dbe8daa9f2efb1
|
4
|
+
data.tar.gz: 6bb5a36e47af0eccaea12e4c298bd410cb8d0270
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80b26de182c475a28339f7c2d3ac420fa1b36298d620118dc45cfcda8f981c49d1afbc39915cda76cafca16255cbbe3dc94c9f4e1ed8a40b853e428f37cdbc3a
|
7
|
+
data.tar.gz: 6ac411f219a66a1c90d71964b86959399dfa5c2c06e84bca6a1122791596e3ec51a948f3a83c9407ffdaead18031199b0c8a4abf30fa5450ea68c771f73a912d
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -155,6 +155,7 @@ end
|
|
155
155
|
default_to: AttributedObject::Unset, # AttributedObject::Unset | any value | AttributedObject::TypeDefaults
|
156
156
|
ignore_extra_keys: false, # false | true
|
157
157
|
coerce_blanks_to_nil: false, # false | true
|
158
|
+
disallow: AttributedObject::Unset, # AttributedObject::Unset | any value
|
158
159
|
}
|
159
160
|
```
|
160
161
|
|
@@ -214,9 +215,24 @@ WithExtraOptions.new(foo: 'asd', something: 'bar') # this will not throw an erro
|
|
214
215
|
This will cause blank strings to equal nil after coercion for all non-string types.
|
215
216
|
For example an integer will not become '' => 0, but instead '' => nil.
|
216
217
|
|
218
|
+
### disallow: nil
|
219
|
+
The global `disallow` option makes it easier to prevent nil in the full model.
|
220
|
+
```ruby
|
221
|
+
class WithExtraOptions
|
222
|
+
include AttributedObject::Strict
|
223
|
+
attributed_object disallow: nil
|
224
|
+
|
225
|
+
attribute :foo
|
226
|
+
end
|
227
|
+
WithExtraOptions.new(foo: nil) # this will not throw an error
|
228
|
+
```
|
229
|
+
|
217
230
|
## Benchmark
|
218
231
|
|
219
232
|
Of course the other gems can do quite a bit more, but this is interesting anyway.
|
220
233
|
(see benchmark_attributed_object.rb)
|
221
234
|
Result: Attributed Object is quite a bit fast (2-3x) than other gems for the specific use cases.
|
222
235
|
|
236
|
+
### keywords
|
237
|
+
|
238
|
+
ruby, AttributedObject, attributed object, attributed_object, value, domain driven design, ddd
|
@@ -13,7 +13,8 @@ module AttributedObject
|
|
13
13
|
@attributed_object_options = {
|
14
14
|
default_to: Unset,
|
15
15
|
ignore_extra_keys: false,
|
16
|
-
coerce_blanks_to_nil: false
|
16
|
+
coerce_blanks_to_nil: false,
|
17
|
+
disallow: Unset
|
17
18
|
}.merge(parent_ops)
|
18
19
|
end
|
19
20
|
|
@@ -33,6 +34,10 @@ module AttributedObject
|
|
33
34
|
end
|
34
35
|
end
|
35
36
|
|
37
|
+
if disallow == Unset
|
38
|
+
disallow = attributed_object_options.fetch(:disallow)
|
39
|
+
end
|
40
|
+
|
36
41
|
_attributed_object_check_type_supported!(type_info)
|
37
42
|
|
38
43
|
attribute_defs[attr_name] = {
|
@@ -157,6 +157,22 @@ describe AttributedObject do
|
|
157
157
|
expect(FooWithExtra.new(bar: 12, not_defined: 'asd').attributes).to eq(bar: 12)
|
158
158
|
end
|
159
159
|
end
|
160
|
+
|
161
|
+
describe 'disallow' do
|
162
|
+
it 'gives default to disallow' do
|
163
|
+
class FooWithExtra
|
164
|
+
include AttributedObject::Strict
|
165
|
+
attributed_object disallow: nil
|
166
|
+
attribute :bar, :integer
|
167
|
+
attribute :has_other_disallow, :integer, disallow: 0
|
168
|
+
end
|
169
|
+
|
170
|
+
expect { FooWithExtra.new(bar: 1, has_other_disallow: 1) }.not_to raise_error
|
171
|
+
expect { FooWithExtra.new(bar: 1, has_other_disallow: nil) }.not_to raise_error
|
172
|
+
expect { FooWithExtra.new(bar: nil, has_other_disallow: 1) }.to raise_error(AttributedObject::DisallowedValueError)
|
173
|
+
expect { FooWithExtra.new(bar: 1, has_other_disallow: 0) }.to raise_error(AttributedObject::DisallowedValueError)
|
174
|
+
end
|
175
|
+
end
|
160
176
|
end
|
161
177
|
|
162
178
|
it 'throws an error for unknown attributes' do
|
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.3'
|
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-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|