Class: Coercible::Coercer::Object

Inherits:
Object
  • Object
show all
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

Options::Undefined

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • coercers (Coercer) (defaults to: Coercer.new)


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

Parameters:

Returns:



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

#coercersCoercer (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

Returns:



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

Examples:

when coercion was successful

coercer[String].coerced?(1) # => true

when coercion was NOT successful

coercer[String].coerced?("foo") # => false

Returns:



136
137
138
# File 'lib/coercible/coercer/object.rb', line 136

def coerced?(value)
  value.kind_of?(self.class.primitive)
end

#inspectString

Inspect the coercer object

Examples:

coercer[Object].inspect # => "<Coercer::Object primitive=Object>"

Returns:



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

Examples:

with an object that does not respond to #to_a or #to_ary

coercer[Object].to_array(value)         # => [ value ]

with an object that responds to #to_a

coercer[Object].to_array(Set[ value ])  # => [ value ]

with n object that responds to #to_ary

coercer[Object].to_array([ value ])     # => [ value ]

Parameters:

  • value (#to_a, #to_ary, Object)
  • value (#to_a, #to_ary, Object)

Returns:



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

Examples:

with a coercible object

coercer[Object].to_hash(key => value)  # => { key => value }

with an object that is not coercible

coercer[Object].to_hash(value)  # => value

Parameters:

Returns:

  • (Hash)

    returns a Hash when the object can be coerced

  • (Object)

    returns the value when the object cannot be coerced



81
82
83
# File 'lib/coercible/coercer/object.rb', line 81

def to_hash(value)
  coerce_with_method(value, :to_hash, __method__)
end

#to_integer(value) ⇒ Integer, Object

Create an Integer from the Object if possible

Examples:

with a coercible object

coercer[Object].to_integer(1)  # => 1

with an object that is not coercible

coercer[Object].to_integer(value)  # => value

Parameters:

Returns:

  • (Integer)

    returns an Integer when the object can be coerced

  • (Object)

    returns the value when the object cannot be coerced



121
122
123
# File 'lib/coercible/coercer/object.rb', line 121

def to_integer(value)
  coerce_with_method(value, :to_int, __method__)
end

#to_string(value) ⇒ String, Object

Create a String from the Object if possible

Examples:

with a coercible object

coercer[Object].to_string("string")  # => "string"

with an object that is not coercible

coercer[Object].to_string(value)  # => value

Parameters:

Returns:

  • (String)

    returns a String when the object can be coerced

  • (Object)

    returns the value when the object cannot be coerced



101
102
103
# File 'lib/coercible/coercer/object.rb', line 101

def to_string(value)
  coerce_with_method(value, :to_str, __method__)
end