reasonable-value 0.2.2 → 0.2.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/.rubocop.yml +3 -0
- data/README.md +14 -10
- data/lib/reasonable/value.rb +14 -4
- data/lib/reasonable/value/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c03ae615d2b65766dfd6fcbe6e575649f240648
|
4
|
+
data.tar.gz: 7c7c90816b4d53336cbdc4e819b5782fb5c16204
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd536842b3c308ee0577180fa05c141f3c7552e0a78e650cd459354b17995906442fa6b57cacac668ad046749452f6f5b9213ba6ec476b4fd462bf8cb42f1326
|
7
|
+
data.tar.gz: e6de6001e9ec707206f7322ee14c59f9b5cd345d4c10ea090d628728bc7c51a736606554fc68845c7296cfae7f68ea467f159168ebcf64b9d325a6581a6c6815
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -36,9 +36,7 @@ By default attributes are mandatory, but corcible (meaning that passing a Float
|
|
36
36
|
when an Integer is expected will **not** raise an error:
|
37
37
|
``` ruby
|
38
38
|
class StandardValue < Reasonable::Value
|
39
|
-
|
40
39
|
attribute :integer, Integer
|
41
|
-
|
42
40
|
end
|
43
41
|
|
44
42
|
p StandardValue.new
|
@@ -57,9 +55,7 @@ p StandardValue.new(integer: 1.1)
|
|
57
55
|
If you want optional attributes, you can say so like that:
|
58
56
|
``` ruby
|
59
57
|
class OptionalValue < Reasonable::Value
|
60
|
-
|
61
58
|
attribute :string, String, optional: true
|
62
|
-
|
63
59
|
end
|
64
60
|
|
65
61
|
p OptionalValue.new
|
@@ -78,9 +74,7 @@ p OptionalValue.new(string: 1.1)
|
|
78
74
|
You are not limited to Integer or String, you can use any type you want:
|
79
75
|
``` ruby
|
80
76
|
class ValueWithCustomType < Reasonable::Value
|
81
|
-
|
82
77
|
attribute :custom, StandardValue
|
83
|
-
|
84
78
|
end
|
85
79
|
|
86
80
|
p ValueWithCustomType.new(custom: StandardValue.new(integer: 1))
|
@@ -90,15 +84,27 @@ p ValueWithCustomType.new(custom: { integer: 1 })
|
|
90
84
|
# => #<ValueWithCustomType:0x007f65ec19f920 @attributes={:custom=>#<StandardValue:0x007f65ec19f358 @attributes={:integer=>1}>}>
|
91
85
|
```
|
92
86
|
|
87
|
+
You can pass a list of types if need be:
|
88
|
+
``` ruby
|
89
|
+
class TypeListValue < Reasonable::Value
|
90
|
+
attribute :boolean, [TrueClass, FalseClass]
|
91
|
+
end
|
92
|
+
|
93
|
+
p TypeListValue.new(boolean: true)
|
94
|
+
# => #<TypeListValue:0x00560d002c7f50 @attributes={:boolean=>true}>
|
95
|
+
p TypeListValue.new(boolean: false)
|
96
|
+
# => #<TypeListValue:0x00560d002c7f50 @attributes={:boolean=>false}>
|
97
|
+
p TypeListValue.new(boolean: 'error')
|
98
|
+
# => TypeError: expected :boolean to be a [TrueClass, FalseClass] but was a String
|
99
|
+
```
|
100
|
+
|
93
101
|
If you define the appropriate method on the class of the attribute,
|
94
102
|
Reasonable::Value will handle casting gracefully:
|
95
103
|
``` ruby
|
96
104
|
class CastableType
|
97
|
-
|
98
105
|
def to_standard_value
|
99
106
|
StandardValue.new(integer: 1)
|
100
107
|
end
|
101
|
-
|
102
108
|
end
|
103
109
|
|
104
110
|
p ValueWithCustomType.new(custom: CastableType.new)
|
@@ -110,7 +116,6 @@ Equality is based on attributes, instead of identity:
|
|
110
116
|
``` ruby
|
111
117
|
p StandardValue.new(integer: 1) == StandardValue.new(integer: 1)
|
112
118
|
# => true
|
113
|
-
|
114
119
|
p StandardValue.new(integer: 1) == StandardValue.new(integer: 2)
|
115
120
|
# => false
|
116
121
|
```
|
@@ -129,4 +134,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
|
|
129
134
|
## License
|
130
135
|
|
131
136
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
132
|
-
|
data/lib/reasonable/value.rb
CHANGED
@@ -21,6 +21,10 @@ module Reasonable
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
+
def to_hash
|
25
|
+
@attributes
|
26
|
+
end
|
27
|
+
|
24
28
|
class << self
|
25
29
|
|
26
30
|
def inherited(subklass)
|
@@ -72,8 +76,12 @@ module Reasonable
|
|
72
76
|
|
73
77
|
class << self
|
74
78
|
|
75
|
-
def call(
|
76
|
-
|
79
|
+
def call(types, value)
|
80
|
+
coerced = Array(types).reduce(nil) do |memo, type|
|
81
|
+
memo || built_in(type, value) || custom(type, value)
|
82
|
+
end
|
83
|
+
|
84
|
+
coerced.nil? and raise(TypeError) or coerced
|
77
85
|
end
|
78
86
|
|
79
87
|
private
|
@@ -82,6 +90,8 @@ module Reasonable
|
|
82
90
|
return unless Kernel.respond_to?(type.to_s)
|
83
91
|
|
84
92
|
Kernel.public_send(type.to_s, value)
|
93
|
+
rescue ArgumentError, TypeError
|
94
|
+
nil
|
85
95
|
end
|
86
96
|
|
87
97
|
def custom(type, value)
|
@@ -91,8 +101,8 @@ module Reasonable
|
|
91
101
|
return value.public_send("to_#{type.to_s.underscore}")
|
92
102
|
end
|
93
103
|
|
94
|
-
|
95
|
-
|
104
|
+
return unless value.is_a?(Hash)
|
105
|
+
return unless type.ancestors.include?(Reasonable::Value)
|
96
106
|
|
97
107
|
type.new(value)
|
98
108
|
end
|