Class: Coercible::Coercer::Object

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

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

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)


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

Parameters:

Returns:



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

#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:



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

Examples:

when coercion was successful

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

when coercion was NOT successful

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

Returns:



138
139
140
# File 'lib/project/coercer/object.rb', line 138

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

#inspectString

Inspect the coercer object

Examples:

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

Returns:



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

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:



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

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



83
84
85
# File 'lib/project/coercer/object.rb', line 83

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



123
124
125
# File 'lib/project/coercer/object.rb', line 123

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



103
104
105
# File 'lib/project/coercer/object.rb', line 103

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