Parent

Coercible::Coercer::Object

Coerce Object values

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.