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 +4 -4
- data/README.md +19 -0
- data/lib/reasonable/value.rb +5 -1
- data/lib/reasonable/value/version.rb +1 -1
- 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: dabaf5fc380bb66806ae3f7e8c30a84c85071878
|
4
|
+
data.tar.gz: 4a9c68e089c088d41b5740de052ad94f34cf5e31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/reasonable/value.rb
CHANGED
@@ -15,7 +15,11 @@ module Reasonable
|
|
15
15
|
@attributes = {}
|
16
16
|
|
17
17
|
self.class.send(:config).each do |name, config|
|
18
|
-
|
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
|
|
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.
|
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-
|
11
|
+
date: 2017-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|