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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e20230e4bbc23eee451104b684a2d23f4b7b7b01
4
- data.tar.gz: 4e18420e5346de8da7b71db5d49b7c22362950c4
3
+ metadata.gz: bc9126c95e9c21a8a9ce36a1cda922227597fa64
4
+ data.tar.gz: 5f55463da5e91facccde403207ab6eaf12f5a950
5
5
  SHA512:
6
- metadata.gz: 97e10af76a774ed5c2a6e8fddb922912d07a22756793d103b5a9fddf3daaaba9e4865b5f0f08063f67d9224db300d5e080eab9e565c2ea3e10229444632528d2
7
- data.tar.gz: 943170d3ca5ac6e4a27210dfe1f3a78b0e403f48ad95a18f4a6e394a2b096c2fc15f40a1f70d768e5323eea2a559971ce41cfeba7ff32325596c4455c0575d64
6
+ metadata.gz: 4627df529f9fc9be213e4a2540b4d288866bef70d5ad7e6a56ee04739b64cdb541d1e6d552f2592531a0d7e450ea4cb90300dab29a7ca88913a162cb345d80f9
7
+ data.tar.gz: f60ddb0144586dddcc56f677621b1991e3e9f110ceeaa9eea4b617cec91c14c60accc17d04bcbd5f2ca673aa51d2af67f656f3ad1efbfbfa6e970c1c670a88fb
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ Gemfile.lock
@@ -0,0 +1,13 @@
1
+ module NestedStruct
2
+ class Configuration
3
+ attr_reader :use_empty_value
4
+
5
+ def initialize
6
+ @use_empty_value = false
7
+ end
8
+
9
+ def use_empty_value=(boolean)
10
+ @use_empty_value = boolean
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ require 'singleton'
2
+
3
+ module NestedStruct
4
+ class Empty
5
+ include Singleton
6
+
7
+ def to_s
8
+ 'NestedStruct::Empty'.freeze
9
+ end
10
+
11
+ alias_method :inspect, :to_s
12
+ end
13
+ end
@@ -9,6 +9,7 @@ module NestedStruct
9
9
 
10
10
  def with(value)
11
11
  return value if value.nil?
12
+ return value if value.is_a?(NestedStruct::Empty)
12
13
  return value if !coerced?
13
14
  coercer.coerce(value)
14
15
  end
@@ -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.with(attributes[field.name])
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
@@ -1,3 +1,3 @@
1
1
  module NestedStruct
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  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.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-14 00:00:00.000000000 Z
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