Class: OpenStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/interfacets/client/utils/open_struct.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ OpenStruct

Returns a new instance of OpenStruct.



18
19
20
21
22
23
24
25
26
27
# File 'lib/interfacets/client/utils/open_struct.rb', line 18

def initialize hash=nil
  @table = {}
  if hash
    hash.each do |k,v|
      k = k.to_sym
      @table[k] = v
      new_ostruct_member(k)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/interfacets/client/utils/open_struct.rb', line 99

def method_missing mid, *args
  mname = mid.to_s
  len = args.length
  if mname.chomp! '='
    if len != 1
      raise ArgumentError, "wrong number of arguments (#{len} for 1)"
    end
    @table[new_ostruct_member(mname)] = args[0]
  elsif len == 0
    @table[mid]
  else
    raise NoMethodError, "undefined method `#{mid}' for #{self}"
  end
end

Instance Method Details

#==(other) ⇒ Object



74
75
76
77
# File 'lib/interfacets/client/utils/open_struct.rb', line 74

def == other
  return false unless other.kind_of?(OpenStruct)
  @table == other.table
end

#[](key) ⇒ Object



79
80
81
# File 'lib/interfacets/client/utils/open_struct.rb', line 79

def [] key
  @table[key.to_sym]
end

#[]=(key, value) ⇒ Object



83
84
85
# File 'lib/interfacets/client/utils/open_struct.rb', line 83

def []= key, value
  @table[new_ostruct_member(key)] = value
end

#delete_field(name) ⇒ Object



68
69
70
71
72
# File 'lib/interfacets/client/utils/open_struct.rb', line 68

def delete_field name
  sym = name.to_sym
  singleton_class.__send__ :remove_method, sym, "#{sym}=".to_sym
  @table.delete sym
end

#each_pair ⇒ Object



39
40
41
42
# File 'lib/interfacets/client/utils/open_struct.rb', line 39

def each_pair
  return to_enum __method__ unless block_given?
  @table.each{|p| yield p}
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/interfacets/client/utils/open_struct.rb', line 87

def eql? other
  return false unless other.kind_of?(OpenStruct)
  @table.eql?(other.table)
end

#hash ⇒ Object



92
93
94
# File 'lib/interfacets/client/utils/open_struct.rb', line 92

def hash
  @table.hash
end

#initialize_copy(orig) ⇒ Object



29
30
31
32
33
# File 'lib/interfacets/client/utils/open_struct.rb', line 29

def initialize_copy orig
  super
  @table = @table.dup
  @table.each_key{|key| new_ostruct_member(key)}
end

#inspect ⇒ Object Also known as: to_s



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/interfacets/client/utils/open_struct.rb', line 54

def inspect
  str = "#<#{self.class}"
  ary = []
  @table.each do |k,v|
    ary << "#{k}=#{v}"
  end
  if 0 < ary.length
    str << ' '
    str << ary.join(', ')
  end
  str << '>'
end

#to_h ⇒ Object



35
36
37
# File 'lib/interfacets/client/utils/open_struct.rb', line 35

def to_h
  @table.dup
end