class PersistentOpenStruct

Constants

VERSION

Public Class Methods

new(hash=nil) click to toggle source

From here on was copied from github.com/ruby/ruby/blob/3c7a96bfa2d21336d985ceda544c4ccabafebed5/lib/ostruct.rb to avoid changes to internals negatively affecting performance.

# File lib/persistent_open_struct.rb, line 23
def initialize(hash=nil)
  @table = {}
  if hash
    hash.each_pair do |k, v|
      k = k.to_sym
      @table[k] = v
      new_ostruct_member(k)
    end
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/persistent_open_struct.rb, line 121
def ==(other)
  return false unless other.class == self.class
  @table == other.table
end
[](name) click to toggle source
# File lib/persistent_open_struct.rb, line 85
def [](name)
  @table[name.to_sym]
end
[]=(name, value) click to toggle source
# File lib/persistent_open_struct.rb, line 89
def []=(name, value)
  modifiable[new_ostruct_member(name)] = value
end
delete_field(name) click to toggle source
# File lib/persistent_open_struct.rb, line 16
def delete_field(name)
  @table.delete(name)
end
each_pair() { |p| ... } click to toggle source
# File lib/persistent_open_struct.rb, line 44
def each_pair
  return to_enum(__method__) { @table.size } unless block_given?
  @table.each_pair{|p| yield p}
end
eql?(other) click to toggle source
# File lib/persistent_open_struct.rb, line 126
def eql?(other)
  return false unless other.class == self.class
  @table.eql?(other.table)
end
hash() click to toggle source
# File lib/persistent_open_struct.rb, line 131
def hash
  @table.hash
end
initialize_copy(orig) click to toggle source
Calls superclass method
# File lib/persistent_open_struct.rb, line 34
def initialize_copy(orig)
  super
  @table = @table.dup
  @table.each_key{|key| new_ostruct_member(key)}
end
inspect() click to toggle source
# File lib/persistent_open_struct.rb, line 95
def inspect
  str = "#<#{self.class}"

  ids = (Thread.current[InspectKey] ||= [])
  if ids.include?(object_id)
    return str << ' ...>'
  end

  ids << object_id
  begin
    first = true
    for k,v in @table
      str << "," unless first
      first = false
      str << " #{k}=#{v.inspect}"
    end
    return str << '>'
  ensure
    ids.pop
  end
end
Also aliased as: to_s
marshal_dump() click to toggle source
# File lib/persistent_open_struct.rb, line 49
def marshal_dump
  @table
end
marshal_load(x) click to toggle source
# File lib/persistent_open_struct.rb, line 53
def marshal_load(x)
  @table = x
  @table.each_key{|key| new_ostruct_member(key)}
end
to_h() click to toggle source
# File lib/persistent_open_struct.rb, line 40
def to_h
  @table.dup
end
to_s()
Alias for: inspect

Protected Instance Methods

modifiable() click to toggle source
# File lib/persistent_open_struct.rb, line 58
def modifiable
  begin
    @modifiable = true
  rescue
    raise RuntimeError, "can't modify frozen #{self.class}", caller(3)
  end
  @table
end
new_ostruct_member(name) click to toggle source

The following 2 methods are altered from the original OpenStruct. Everything else is copied from OpenStruct on Ruby 2.2. (The performance enhancements in 2.3 don't make sense for this implementation.)

# File lib/persistent_open_struct.rb, line 6
def new_ostruct_member(name)
  name = name.to_sym
  unless respond_to?(name)
    self.class.send(:define_method, name) { @table[name] }
    self.class.send(:define_method, "#{name}=") { |x| modifiable[name] = x }
  end
  name
end