Method: Struct.from_h

Defined in:
lib/casual_support/struct/from_h.rb

.from_h(attributes) ⇒ Struct

Constructs an instance of a subclass of Struct, and assigns the values of the given attributes Hash to the instance.

This method is intended for use only with subclasses of Struct which do not alter the default signature of the initialize method. Specifically, the initialize method must support invocation with no arguments.

Examples:

Point = Struct.new(:x, :y, :z)

Point.from_h(x: 10, y: 20, z: 30)  # == Point.new(10, 20, 30)

Parameters:

Returns:

See Also:



22
23
24
25
# File 'lib/casual_support/struct/from_h.rb', line 22

def self.from_h(attributes)
  raise "Struct.from_h is for use only with subclasses of Struct" if self == Struct
  self.new.assign_attributes(attributes)
end