nested_struct 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/lib/nested_struct/configuration.rb +13 -0
- data/lib/nested_struct/empty.rb +13 -0
- data/lib/nested_struct/field.rb +1 -0
- data/lib/nested_struct/interface.rb +21 -6
- data/lib/nested_struct/version.rb +1 -1
- data/lib/nested_struct.rb +15 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc9126c95e9c21a8a9ce36a1cda922227597fa64
|
4
|
+
data.tar.gz: 5f55463da5e91facccde403207ab6eaf12f5a950
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4627df529f9fc9be213e4a2540b4d288866bef70d5ad7e6a56ee04739b64cdb541d1e6d552f2592531a0d7e450ea4cb90300dab29a7ca88913a162cb345d80f9
|
7
|
+
data.tar.gz: f60ddb0144586dddcc56f677621b1991e3e9f110ceeaa9eea4b617cec91c14c60accc17d04bcbd5f2ca673aa51d2af67f656f3ad1efbfbfa6e970c1c670a88fb
|
data/.gitignore
CHANGED
data/lib/nested_struct/field.rb
CHANGED
@@ -24,20 +24,27 @@ module NestedStruct
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def to_hash
|
27
|
-
Hash[
|
28
|
-
self.class.fields.map do |field|
|
29
|
-
[field.name.to_sym, primitivize(public_send(field.name))]
|
30
|
-
end
|
31
|
-
]
|
27
|
+
Hash[map_fields]
|
32
28
|
end
|
33
29
|
|
34
30
|
private
|
35
31
|
|
32
|
+
def map_fields
|
33
|
+
mapped_fields = []
|
34
|
+
|
35
|
+
self.class.fields.each do |field|
|
36
|
+
result = [field.name.to_sym, primitivize(public_send(field.name))]
|
37
|
+
mapped_fields << result if !result.last.is_a?(NestedStruct::Empty)
|
38
|
+
end
|
39
|
+
|
40
|
+
mapped_fields
|
41
|
+
end
|
42
|
+
|
36
43
|
def assign_field_values(attributes = {})
|
37
44
|
self.class.fields.each do |field|
|
38
45
|
instance_variable_set(
|
39
46
|
"@#{field.name}",
|
40
|
-
field
|
47
|
+
field_value(field, attributes)
|
41
48
|
)
|
42
49
|
end
|
43
50
|
end
|
@@ -48,5 +55,13 @@ module NestedStruct
|
|
48
55
|
return value.to_hash if value.respond_to?(:to_hash)
|
49
56
|
value
|
50
57
|
end
|
58
|
+
|
59
|
+
def field_value(field, attributes)
|
60
|
+
if NestedStruct.configuration.use_empty_value
|
61
|
+
field.with(attributes.fetch(field.name, NestedStruct::EMPTY))
|
62
|
+
else
|
63
|
+
field.with(attributes[field.name])
|
64
|
+
end
|
65
|
+
end
|
51
66
|
end
|
52
67
|
end
|
data/lib/nested_struct.rb
CHANGED
@@ -4,10 +4,25 @@ require 'nested_struct/errors/invalid_coercer_expression'
|
|
4
4
|
require 'nested_struct/errors/invalid_coercer_target'
|
5
5
|
require 'nested_struct/errors/invalid_coercer_value'
|
6
6
|
|
7
|
+
require 'nested_struct/configuration'
|
8
|
+
require 'nested_struct/empty'
|
9
|
+
|
7
10
|
require 'nested_struct/coercer'
|
8
11
|
require 'nested_struct/field'
|
9
12
|
|
10
13
|
require 'nested_struct/interface'
|
11
14
|
|
12
15
|
module NestedStruct
|
16
|
+
EMPTY = self::Empty.instance
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def configuration
|
20
|
+
@configuration ||= Configuration.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def configure
|
24
|
+
yield(configuration) if block_given?
|
25
|
+
configuration
|
26
|
+
end
|
27
|
+
end
|
13
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Krzysztof Buszewicz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-06-
|
11
|
+
date: 2018-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,6 +70,8 @@ files:
|
|
70
70
|
- bin/setup
|
71
71
|
- lib/nested_struct.rb
|
72
72
|
- lib/nested_struct/coercer.rb
|
73
|
+
- lib/nested_struct/configuration.rb
|
74
|
+
- lib/nested_struct/empty.rb
|
73
75
|
- lib/nested_struct/errors/invalid_coercer_expression.rb
|
74
76
|
- lib/nested_struct/errors/invalid_coercer_target.rb
|
75
77
|
- lib/nested_struct/errors/invalid_coercer_value.rb
|