class OpenFastStruct
Public Class Methods
new(args = {})
click to toggle source
# File lib/ofstruct.rb, line 4 def initialize(args = {}) @members = {} update(args) end
Public Instance Methods
==(other)
click to toggle source
# File lib/ofstruct.rb, line 44 def ==(other) other.is_a?(self.class) && to_h == other.to_h end
delete_field(key)
click to toggle source
# File lib/ofstruct.rb, line 9 def delete_field(key) assign(key, self.class.new) end
each_pair()
click to toggle source
# File lib/ofstruct.rb, line 13 def each_pair @members.each_pair end
inspect()
click to toggle source
# File lib/ofstruct.rb, line 35 def inspect "#<#{self.class}#{@members.map { |key, value| " :#{key}=#{value.inspect}" }.join}>" end
Also aliased as: to_s
to_ary()
click to toggle source
# File lib/ofstruct.rb, line 40 def to_ary nil end
to_h()
click to toggle source
# File lib/ofstruct.rb, line 22 def to_h @members.merge(@members) do |_, value| case value when Array value.map(&:to_h) when self.class value.to_h else value end end end
update(args)
click to toggle source
# File lib/ofstruct.rb, line 17 def update(args) ensure_hash!(args) args.each { |key, value| assign(key, value) } end
Private Instance Methods
assign(key, value)
click to toggle source
# File lib/ofstruct.rb, line 64 def assign(key, value) @members[key.to_sym] = process(value) end
ensure_hash!(args)
click to toggle source
# File lib/ofstruct.rb, line 50 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 54 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
process(data)
click to toggle source
# File lib/ofstruct.rb, line 68 def process(data) case data when Hash self.class.new(data) when Array data.map { |element| process(element) } else data end end