Sha256: 8e201f0d05dc2b13fceaff6494a73f9a72293f687ef7bc4f1a0cc63dbd529c56

Contents?: true

Size: 743 Bytes

Versions: 1

Compression:

Stored size: 743 Bytes

Contents

require_relative "assign_attributes"

class Struct

  # Constructs an instance of a subclass of Struct, and assigns the
  # values of the given attribute 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.
  #
  # @see Struct#assign_attributes
  #
  # @example
  #   Point = Struct.new(:x, :y, :z)
  #
  #   Point.from_h(x: 10, y: 20, z: 30)  # == Point.new(10, 20, 30)
  #
  # @param attributes [Hash<Symbol, Object>, Hash<String, Object>]
  # @return [Struct]
  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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
casual_support-3.0.2 lib/casual_support/struct/from_h.rb