reasonable-value 0.2.3 → 0.2.4

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: 4c03ae615d2b65766dfd6fcbe6e575649f240648
4
- data.tar.gz: 7c7c90816b4d53336cbdc4e819b5782fb5c16204
3
+ metadata.gz: 1e5918ca928d7840741f459d98f1c35f239dc9d8
4
+ data.tar.gz: c0acde6480ae1bdc01b610df5c1a52bdd5f9a754
5
5
  SHA512:
6
- metadata.gz: bd536842b3c308ee0577180fa05c141f3c7552e0a78e650cd459354b17995906442fa6b57cacac668ad046749452f6f5b9213ba6ec476b4fd462bf8cb42f1326
7
- data.tar.gz: e6de6001e9ec707206f7322ee14c59f9b5cd345d4c10ea090d628728bc7c51a736606554fc68845c7296cfae7f68ea467f159168ebcf64b9d325a6581a6c6815
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
  ```
@@ -11,7 +11,7 @@ module Reasonable
11
11
  @attributes <=> other.instance_variable_get(:@attributes)
12
12
  end
13
13
 
14
- def initialize(**attributes)
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
- if value.respond_to?("to_#{type.to_s.underscore}")
101
- return value.public_send("to_#{type.to_s.underscore}")
102
- end
104
+ Try.(MethodName.(type), value) || reasonable(type, value)
105
+ end
103
106
 
104
- return unless value.is_a?(Hash)
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
@@ -1,7 +1,7 @@
1
1
  module Reasonable
2
2
  class Value
3
3
 
4
- VERSION = '0.2.3'.freeze
4
+ VERSION = '0.2.4'.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.3
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-03 00:00:00.000000000 Z
11
+ date: 2017-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport