Sha256: 7f17782f17a55c2bb9201cccdd1000f4d3763514834392fd1938dd89b38d06cc

Contents?: true

Size: 609 Bytes

Versions: 1

Compression:

Stored size: 609 Bytes

Contents

# Allow structs to be initialised using a hash
module SolidStruct
  VERSION = "0.1.0"

  class NamedStruct < Struct
    # Override the initialize to handle hashes of named parameters
    def initialize *args
      opts = args.last.is_a?(Hash) ? args.pop : Hash.new
      super *args
      opts.each_pair do |k, v|
        self.send "#{k}=", v
      end
    end
  end

  def self.new(*args)
    NamedStruct.new(*args)
  end

  def self.build(hash)
    raise ArgumentError.new("Arguments must be defined as a hash") unless hash.is_a? Hash

    klass = NamedStruct.new(*hash.keys)
    klass.new(hash)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solid-struct-0.1.0 lib/solid_struct.rb