Sha256: e3579d106df5f65da67ace4174cc1da7df70473a1233a6fb1300fb3f647d3ca6

Contents?: true

Size: 1.12 KB

Versions: 26

Compression:

Stored size: 1.12 KB

Contents

class OpenStruct
  def initialize(hash = nil)
    @table = {}

    hash.each_pair {|key, value|
      @table[key.to_sym] = value
    } if hash
  end

  def [](name)
    @table[name.to_sym]
  end

  def []=(name, value)
    @table[name.to_sym] = value
  end

  def method_missing(name, *args)
    if name.end_with? '='
      @table[name[0 .. -2].to_sym] = args[0]
    else
      @table[name.to_sym]
    end
  end

  def each_pair
    return enum_for :each_pair unless block_given?

    @table.each_pair {|pair|
      yield pair
    }
  end

  def ==(other)
    return false unless other.is_a?(OpenStruct)

    @table == other.instance_variable_get(:@table)
  end

  def ===(other)
    return false unless other.is_a?(OpenStruct)

    @table === other.instance_variable_get(:@table)
  end

  def eql?(other)
    return false unless other.is_a?(OpenStruct)

    @table.eql? other.instance_variable_get(:@table)
  end

  def to_h
    @table.dup
  end

  def to_n
    @table.to_n
  end

  def hash
    @table.hash
  end

  def inspect
    "#<#{self.class}: #{each_pair.map {|name, value|
      "#{name}=#{self[name].inspect}"
    }.join(" ")}>"
  end
end

Version data entries

26 entries across 26 versions & 3 rubygems

Version Path
opal-0.8.1 stdlib/ostruct.rb
opal-0.8.1.rc1 stdlib/ostruct.rb
opal-wedge-0.9.0.dev stdlib/ostruct.rb
opal-0.8.0 stdlib/ostruct.rb
opal-0.8.0.rc3 stdlib/ostruct.rb
opal-0.8.0.rc2 stdlib/ostruct.rb
opal-0.8.0.rc1 stdlib/ostruct.rb
opal-0.8.0.beta1 stdlib/ostruct.rb
opal-0.7.2 stdlib/ostruct.rb
opal-0.7.1 stdlib/ostruct.rb
opal-0.7.0 stdlib/ostruct.rb
opal-0.7.0.rc1 stdlib/ostruct.rb
opal-0.7.0.beta3 stdlib/ostruct.rb
opal-0.6.3 stdlib/ostruct.rb
opal-0.7.0.beta2 stdlib/ostruct.rb
opal-cj-0.7.0.beta2 stdlib/ostruct.rb
opal-cj-0.7.0.beta1 stdlib/ostruct.rb
opal-0.7.0.beta1 stdlib/ostruct.rb
opal-0.6.2 stdlib/ostruct.rb
opal-0.6.1 stdlib/ostruct.rb