Module: KeyDial::Coercion::Structs::ClassMethods

Defined in:
lib/key_dial/coercion.rb

Instance Method Summary collapse

Instance Method Details

#from(obj, type_class = nil) ⇒ Object

Allows you to do Struct.from(obj) to instantiate a Struct using keys/values from any object intelligently.

Parameters:

  • type_class (defaults to: nil)

    If a sub-class of Struct is provided, this sub-class will be instantiated



105
106
107
108
109
110
111
112
113
# File 'lib/key_dial/coercion.rb', line 105

def from(obj, type_class = nil)
  if !obj.is_a?(Struct) && !obj.is_a?(Hash) && !obj.is_a?(Array) && type_class == nil
    s = EMPTY.dup
    s[0] = obj
    return s
  else
    return Coercion::Structs.create(obj, type_class)
  end
end