reasonable-value 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/reasonable/value.rb +23 -5
- 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: 1e5918ca928d7840741f459d98f1c35f239dc9d8
|
4
|
+
data.tar.gz: c0acde6480ae1bdc01b610df5c1a52bdd5f9a754
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99a2576820e6214f95dfff2bb98e38889b170f055885f512a5bfaaac8c4fb0803356e431d17e8e5b8bcb73b140b19741591a088a862c6e2120b67f747397a56e
|
7
|
+
data.tar.gz: 42964154cac368d69d5218992f07ab12a79011ab94f53b8512fdf43dfe4e0a351a7360b436e2be994499b578e837e6faa36c8b2aec3fe8060211106c94822239
|
data/README.md
CHANGED
@@ -107,6 +107,9 @@ class CastableType
|
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
|
+
p StandardValue.new(CastableType.new)
|
111
|
+
# => #<StandardValue:0x005598945ba928 @attributes={:integer=>1}>
|
112
|
+
|
110
113
|
p ValueWithCustomType.new(custom: CastableType.new)
|
111
114
|
# => #<ValueWithCustomType:0x007f65ec1a6590 @attributes={:custom=>#<StandardValue:0x007f65ec1a5bb8 @attributes={:integer=>1}>}>
|
112
115
|
```
|
data/lib/reasonable/value.rb
CHANGED
@@ -11,7 +11,7 @@ module Reasonable
|
|
11
11
|
@attributes <=> other.instance_variable_get(:@attributes)
|
12
12
|
end
|
13
13
|
|
14
|
-
def initialize(
|
14
|
+
def initialize(attributes = {})
|
15
15
|
@attributes = {}
|
16
16
|
|
17
17
|
self.class.send(:config).each do |name, config|
|
@@ -27,6 +27,10 @@ module Reasonable
|
|
27
27
|
|
28
28
|
class << self
|
29
29
|
|
30
|
+
def new(object = {})
|
31
|
+
Try.(MethodName.(self), object) || super(object)
|
32
|
+
end
|
33
|
+
|
30
34
|
def inherited(subklass)
|
31
35
|
config.each do |name, config|
|
32
36
|
subklass.attribute(name, config[:type], options: config[:options])
|
@@ -97,12 +101,12 @@ module Reasonable
|
|
97
101
|
def custom(type, value)
|
98
102
|
return value if value.is_a?(type)
|
99
103
|
|
100
|
-
|
101
|
-
|
102
|
-
end
|
104
|
+
Try.(MethodName.(type), value) || reasonable(type, value)
|
105
|
+
end
|
103
106
|
|
104
|
-
|
107
|
+
def reasonable(type, value)
|
105
108
|
return unless type.ancestors.include?(Reasonable::Value)
|
109
|
+
return unless value.is_a?(Hash)
|
106
110
|
|
107
111
|
type.new(value)
|
108
112
|
end
|
@@ -112,5 +116,19 @@ module Reasonable
|
|
112
116
|
end
|
113
117
|
private_constant :Coercer
|
114
118
|
|
119
|
+
module Try
|
120
|
+
def self.call(method_name, object)
|
121
|
+
object.public_send(method_name) if object.respond_to?(method_name)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
private_constant :Try
|
125
|
+
|
126
|
+
module MethodName
|
127
|
+
def self.call(klass)
|
128
|
+
"to_#{klass.to_s.split('::').last.underscore}"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
private_constant :MethodName
|
132
|
+
|
115
133
|
end
|
116
134
|
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.
|
4
|
+
version: 0.2.4
|
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-04-
|
11
|
+
date: 2017-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|