Sha256: a43ec8b7c2a5a337d36ec53ebdb63cfdadb53db02e055fa1e5463c606dcdc10f

Contents?: true

Size: 732 Bytes

Versions: 1

Compression:

Stored size: 732 Bytes

Contents

module AltStruct
  attr_reader :table

  def initialize(pairs = {})
    @table ||= {}
    for key, value in pairs
      @table.merge! define_accessor key.to_sym, value
    end unless pairs == {}
  end

  def define_accessor(key, value)
    define_singleton_method(key) { @table[key] }
    define_singleton_method(:"#{key}=") { |v| @table[key] = v }
    { key => value }.freeze
  end

  def load(pairs)
    for key, value in pairs
      @table.merge! define_accessor key.to_sym, value
    end unless pairs == {}
  end

  def dump(*keys)
    if keys == [] then @table else @table.keep_if { |k| keys.include? k } end
  end

  def inspect
    "#<#{self.class}:#{object_id} #{dump.map { |k,v| "#{k}=#{v.inspect}" }.join ' '}>"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
astruct-1.0.0 lib/astruct.rb