class OpenFastStruct
Public Class Methods
new(args = {})
click to toggle source
# File lib/ofstruct.rb, line 2 def initialize(args = {}) @members = {} update(args) end
Public Instance Methods
==(other)
click to toggle source
# File lib/ofstruct.rb, line 33 def ==(other) other.is_a?(self.class) && self.to_h == other.to_h end
delete_field(key)
click to toggle source
# File lib/ofstruct.rb, line 7 def delete_field(key) assign(key, self.class.new) end
each_pair()
click to toggle source
# File lib/ofstruct.rb, line 11 def each_pair @members.each_pair end
inspect()
click to toggle source
# File lib/ofstruct.rb, line 24 def inspect "#<#{self.class}" + @members.map { |k, v| " :#{k}=#{v.inspect}" }.join + ">" end
Also aliased as: to_s
to_ary()
click to toggle source
# File lib/ofstruct.rb, line 29 def to_ary nil end
to_h()
click to toggle source
# File lib/ofstruct.rb, line 20 def to_h @members.merge(@members) { |_, v| v.is_a?(self.class) ? v.to_h : v } end
update(args)
click to toggle source
# File lib/ofstruct.rb, line 15 def update(args) ensure_hash!(args) args.each { |k, v| assign(k, v) } end
Private Instance Methods
assign(key, value)
click to toggle source
# File lib/ofstruct.rb, line 53 def assign(key, value) @members[key.to_sym] = value end
ensure_hash!(args)
click to toggle source
# File lib/ofstruct.rb, line 39 def ensure_hash!(args) raise ArgumentError unless args.is_a?(Hash) end
method_missing(name, *args)
click to toggle source
# File lib/ofstruct.rb, line 43 def method_missing(name, *args) @members.fetch(name) do if name[-1] == "=" assign(name[0..-2], args.first) else delete_field(name) end end end