Class: Coercible::Coercer::Object
- Inherits:
-
Object
- Object
- Coercible::Coercer::Object
- Extended by:
- Options, TypeLookup
- Defined in:
- lib/project/coercer/object.rb
Overview
Coerce Object values
Direct Known Subclasses
Array, Date, DateTime, FalseClass, Hash, Numeric, String, Symbol, Time, TrueClass
Constant Summary collapse
- COERCION_METHOD_REGEXP =
/\Ato_/.freeze
Constants included from Options
Constants included from TypeLookup
Instance Attribute Summary collapse
-
#coercers ⇒ Coercer
readonly
private
Return coercers object.
Instance Method Summary collapse
-
#coerced?(value) ⇒ TrueClass, FalseClass
Return if the value was successfuly coerced.
-
#initialize(coercers = Coercer.new) ⇒ undefined
constructor
private
Initialize a new coercer instance.
-
#inspect ⇒ String
Inspect the coercer object.
-
#to_array(value) ⇒ Array
Create an Array from any Object.
-
#to_hash(value) ⇒ Hash, Object
Create a Hash from the Object if possible.
-
#to_integer(value) ⇒ Integer, Object
Create an Integer from the Object if possible.
-
#to_string(value) ⇒ String, Object
Create a String from the Object if possible.
Methods included from Options
accept_options, accepted_options, extended, options
Methods included from TypeLookup
Constructor Details
#initialize(coercers = Coercer.new) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a new coercer instance
30 31 32 |
# File 'lib/project/coercer/object.rb', line 30 def initialize(coercers = Coercer.new) @coercers = coercers end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (private)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Passthrough given value
165 166 167 168 169 170 171 |
# File 'lib/project/coercer/object.rb', line 165 def method_missing(method, *args) if method.to_s =~ COERCION_METHOD_REGEXP && args.size == 1 args.first else super end end |
Instance Attribute Details
#coercers ⇒ Coercer (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return coercers object
21 22 23 |
# File 'lib/project/coercer/object.rb', line 21 def coercers @coercers end |
Instance Method Details
#coerced?(value) ⇒ TrueClass, FalseClass
Return if the value was successfuly coerced
138 139 140 |
# File 'lib/project/coercer/object.rb', line 138 def coerced?(value) value.kind_of?(self.class.primitive) end |
#inspect ⇒ String
Inspect the coercer object
42 43 44 |
# File 'lib/project/coercer/object.rb', line 42 def inspect "#<#{self.class} primitive=#{self.class.primitive}>" end |
#to_array(value) ⇒ Array
Create an Array from any Object
63 64 65 |
# File 'lib/project/coercer/object.rb', line 63 def to_array(value) Array(value) end |
#to_hash(value) ⇒ Hash, Object
Create a Hash from the Object if possible
83 84 85 |
# File 'lib/project/coercer/object.rb', line 83 def to_hash(value) coerce_with_method(value, :to_hash, __method__) end |