Class: Coercible::Coercer::Object
- Inherits:
-
Object
- Object
- Coercible::Coercer::Object
- Extended by:
- Options, TypeLookup
- Defined in:
- lib/coercible/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
determine_type, extended, primitive
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
28 29 30 |
# File 'lib/coercible/coercer/object.rb', line 28 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
163 164 165 166 167 168 169 |
# File 'lib/coercible/coercer/object.rb', line 163 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
19 20 21 |
# File 'lib/coercible/coercer/object.rb', line 19 def coercers @coercers end |
Instance Method Details
#coerced?(value) ⇒ TrueClass, FalseClass
Return if the value was successfuly coerced
136 137 138 |
# File 'lib/coercible/coercer/object.rb', line 136 def coerced?(value) value.kind_of?(self.class.primitive) end |
#inspect ⇒ String
Inspect the coercer object
40 41 42 |
# File 'lib/coercible/coercer/object.rb', line 40 def inspect "#<#{self.class} primitive=#{self.class.primitive}>" end |
#to_array(value) ⇒ Array
Create an Array from any Object
61 62 63 |
# File 'lib/coercible/coercer/object.rb', line 61 def to_array(value) Array(value) end |
#to_hash(value) ⇒ Hash, Object
Create a Hash from the Object if possible
81 82 83 |
# File 'lib/coercible/coercer/object.rb', line 81 def to_hash(value) coerce_with_method(value, :to_hash, __method__) end |