reasonable-value 0.2.5 → 0.2.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1ab93b62305d6676a78a210a30ce9e9dd2735b2
4
- data.tar.gz: 81d160d2827e462b6f55c5fe5345ce8337b86f81
3
+ metadata.gz: dabaf5fc380bb66806ae3f7e8c30a84c85071878
4
+ data.tar.gz: 4a9c68e089c088d41b5740de052ad94f34cf5e31
5
5
  SHA512:
6
- metadata.gz: a7f01564ec99f4af3af13a612cb1abc5b9494053e70ed6d55c3cf49807c2af3086ab3c50edfbfb1d7df79757220f7a8f4d12eb04fd1c1f71ac452697d6227869
7
- data.tar.gz: 188ac754b763c71342c2ef2bd5014efc9777d1a3b7555d27a38daf2191791af5e7114ecb870cfcc081f0d7e3092383fbc2fe55480e7232b466355e345487e8c4
6
+ metadata.gz: 65389977c20d82fa1c662fd87b95401fde700df89065e85c92e1b5b503a5371bfcb2cf423b075a4e21cd5a05da50523807db163022348366842046c240484933
7
+ data.tar.gz: b11ce3ccc8b4e0151ac2b8cd37e55c5d8e9c84f4b5fbd107ed69d57028a3b5f4c36fc1c88a4db974981d4ca14489d42b96e6d690c03ceda6a6e47f248e113f6b
data/README.md CHANGED
@@ -71,6 +71,25 @@ p OptionalValue.new(string: 1.1)
71
71
  # => #<OptionalValue:0x007f65ec1792e8 @attributes={:string=>"1.1"}>
72
72
  ```
73
73
 
74
+ Additionally, you can specify a default value for optional attributes:
75
+ ``` ruby
76
+ class OptionalValueWithDefault < Reasonable::Value
77
+ attribute :string, String, optional: true, default: 'foo'
78
+ end
79
+
80
+ p OptionalValueWithDefault.new
81
+ # => #<OptionalValueWithDefault:0x0055ecec2ae2c8 @attributes={:string=>"foo"}>
82
+
83
+ p OptionalValueWithDefault.new(string: nil)
84
+ # => #<OptionalValueWithDefault:0x007f65ec16c430 @attributes={:string=>"foo"}>
85
+
86
+ p OptionalValueWithDefault.new(string: 'string')
87
+ # => #<OptionalValueWithDefault:0x007f65ec174f18 @attributes={:string=>"string"}>
88
+
89
+ p OptionalValueWithDefault.new(string: 1.1)
90
+ # => #<OptionalValueWithDefault:0x007f65ec1792e8 @attributes={:string=>"1.1"}>
91
+ ```
92
+
74
93
  You are not limited to Integer or String, you can use any type you want:
75
94
  ``` ruby
76
95
  class ValueWithCustomType < Reasonable::Value
@@ -15,7 +15,11 @@ module Reasonable
15
15
  @attributes = {}
16
16
 
17
17
  self.class.send(:config).each do |name, config|
18
- next if attributes[name].nil? && config[:options][:optional]
18
+ options = config[:options]
19
+ if options[:optional]
20
+ attributes[name] ||= options[:default]
21
+ next if attributes[name].nil?
22
+ end
19
23
 
20
24
  type_error(name, config[:type], NilClass) if attributes[name].nil?
21
25
 
@@ -1,7 +1,7 @@
1
1
  module Reasonable
2
2
  class Value
3
3
 
4
- VERSION = '0.2.5'.freeze
4
+ VERSION = '0.2.6'.freeze
5
5
 
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reasonable-value
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Larrieu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-03 00:00:00.000000000 Z
11
+ date: 2017-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport