class Coercible::Coercer::Object

Coerce Object values

Constants

COERCION_METHOD_REGEXP

Attributes

coercers[R]

Return coercers object

@return [Coercer]

@api private

Public Class Methods

new(coercers = Coercer.new) click to toggle source

Initialize a new coercer instance

@param [Coercer] coercers

@return [undefined]

@api private

# File lib/coercible/coercer/object.rb, line 28
def initialize(coercers = Coercer.new)
  @coercers = coercers
end

Public Instance Methods

coerced?(value) click to toggle source

Return if the value was successfuly coerced

@example when coercion was successful

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

@example when coercion was NOT successful

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

@return [TrueClass,FalseClass]

@api public

# File lib/coercible/coercer/object.rb, line 136
def coerced?(value)
  value.kind_of?(self.class.primitive)
end
inspect() click to toggle source

Inspect the coercer object

@example

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

@return [String]

@api public

# File lib/coercible/coercer/object.rb, line 40
def inspect
  "#<#{self.class} primitive=#{self.class.primitive}>"
end
to_array(value) click to toggle source

Create an Array from any Object

@example with an object that does not respond to to_a or to_ary

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

@example with an object that responds to to_a

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

@example with n object that responds to to_ary

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

@param [#to_a,#to_ary,Object] value @param [#to_a,#to_ary,Object] value

@return [Array]

@api public

# File lib/coercible/coercer/object.rb, line 61
def to_array(value)
  Array(value)
end
to_hash(value) click to toggle source

Create a Hash from the Object if possible

@example with a coercible object

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

@example with an object that is not coercible

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

@param [#to_hash, Object] value

@return [Hash]

returns a Hash when the object can be coerced

@return [Object]

returns the value when the object cannot be coerced

@api public

# File lib/coercible/coercer/object.rb, line 81
def to_hash(value)
  coerce_with_method(value, :to_hash, __method__)
end
to_integer(value) click to toggle source

Create an Integer from the Object if possible

@example with a coercible object

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

@example with an object that is not coercible

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

@param [#to_int, Object] value

@return [Integer]

returns an Integer when the object can be coerced

@return [Object]

returns the value when the object cannot be coerced

@api public

# File lib/coercible/coercer/object.rb, line 121
def to_integer(value)
  coerce_with_method(value, :to_int, __method__)
end
to_string(value) click to toggle source

Create a String from the Object if possible

@example with a coercible object

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

@example with an object that is not coercible

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

@param [#to_str, Object] value

@return [String]

returns a String when the object can be coerced

@return [Object]

returns the value when the object cannot be coerced

@api public

# File lib/coercible/coercer/object.rb, line 101
def to_string(value)
  coerce_with_method(value, :to_str, __method__)
end

Private Instance Methods

coerce_with_method(value, method, ref_method) click to toggle source

Try to use native coercion method on the given value

@param [Object] value

@param [Symbol] method

@return [Object]

@api private

# File lib/coercible/coercer/object.rb, line 180
def coerce_with_method(value, method, ref_method)
  value.respond_to?(method) ? value.public_send(method) : raise_unsupported_coercion(value, ref_method)
end
method_missing(method, *args) click to toggle source

Passthrough given value

@param [Object] value

@return [Object]

@api private

Calls superclass method
# 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
raise_unsupported_coercion(value, method) click to toggle source

Raise an unsupported coercion error

@raises [UnsupportedCoercion]

@return [undefined]

@api private

# File lib/coercible/coercer/object.rb, line 149
def raise_unsupported_coercion(value, method)
  raise(
    UnsupportedCoercion,
    "#{self.class}##{method} doesn't know how to coerce #{value.inspect}"
  )
end