reasonable-value 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c103f670ede4f23cadbc3e1e432583a69b342be5
4
- data.tar.gz: dccadbe9c9f516baddfb8eea342792d262f976d8
3
+ metadata.gz: 4c03ae615d2b65766dfd6fcbe6e575649f240648
4
+ data.tar.gz: 7c7c90816b4d53336cbdc4e819b5782fb5c16204
5
5
  SHA512:
6
- metadata.gz: 40be5603cdd99e0e5a82a8f361ef9ca05a2063c1e59f276ddf4b8030582eee5803c45257d2d43100a5017bcadde634fc11682c15c28e62003a480f2a1e5dc10e
7
- data.tar.gz: 1b48fd57b435def353cacc08459eb770dee0fbd7b0059994d85c4d86fa464f019b1dfdc4d54f295b223aff83cadc3f8b9109d14df93f199ec5e93da65dd0d10d
6
+ metadata.gz: bd536842b3c308ee0577180fa05c141f3c7552e0a78e650cd459354b17995906442fa6b57cacac668ad046749452f6f5b9213ba6ec476b4fd462bf8cb42f1326
7
+ data.tar.gz: e6de6001e9ec707206f7322ee14c59f9b5cd345d4c10ea090d628728bc7c51a736606554fc68845c7296cfae7f68ea467f159168ebcf64b9d325a6581a6c6815
@@ -23,6 +23,9 @@ Metrics/BlockLength:
23
23
  Exclude:
24
24
  - 'spec/**/*'
25
25
 
26
+ Style/AndOr:
27
+ Enabled: false
28
+
26
29
  Style/AlignParameters:
27
30
  EnforcedStyle: with_first_parameter
28
31
 
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
-
@@ -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(type, value)
76
- built_in(type, value) || custom(type, value)
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
- raise TypeError unless value.is_a?(Hash)
95
- raise TypeError unless type.ancestors.include?(Reasonable::Value)
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
@@ -1,7 +1,7 @@
1
1
  module Reasonable
2
2
  class Value
3
3
 
4
- VERSION = '0.2.2'.freeze
4
+ VERSION = '0.2.3'.freeze
5
5
 
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reasonable-value
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Larrieu