Class: OpenStruct
  
  
  
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
  
  Constructor Details
  
    
  
  
    #initialize(hash = nil)  ⇒ OpenStruct 
  
  
  
  
    Returns a new instance of OpenStruct.
   
 
  
  
    | 
2
3
4
5
6
7
8
9
10 | # File 'opal/stdlib/ostruct.rb', line 2
def initialize(hash = nil)
  @table = {}
  if hash
    hash.each_pair do |key, value|
      @table[new_ostruct_member(key)] = value
    end
  end
end | 
 
  
 
  Dynamic Method Handling
  
    This class handles dynamic methods through the method_missing method
    
  
  
    
  
  
    #method_missing(name, *args)  ⇒ Object 
  
  
  
  
    | 
20
21
22
23
24
25
26
27
28
29
30
31
32 | # File 'opal/stdlib/ostruct.rb', line 20
def method_missing(name, *args)
  if args.length > 2
    raise NoMethodError.new("undefined method `#{name}' for #<OpenStruct>", name)
  end
  if name.end_with? '='
    if args.length != 1
      raise ArgumentError, 'wrong number of arguments (0 for 1)'
    end
    @table[new_ostruct_member(name[0..-2])] = args[0]
  else
    @table[name.to_sym]
  end
end | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    Returns the value of attribute table.
   
 
  
  
    | 
72
73
74 | # File 'opal/stdlib/ostruct.rb', line 72
def table
  @table
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #==(other)  ⇒ Object 
  
  
  
  
    | 
42
43
44
45
46 | # File 'opal/stdlib/ostruct.rb', line 42
def ==(other)
  return false unless other.is_a?(OpenStruct)
  @table == other.instance_variable_get(:@table)
end | 
 
    
      
  
  
    #===(other)  ⇒ Object 
  
  
  
  
    | 
48
49
50
51
52 | # File 'opal/stdlib/ostruct.rb', line 48
def ===(other)
  return false unless other.is_a?(OpenStruct)
  @table === other.instance_variable_get(:@table)
end | 
 
    
      
  
  
    | 
12
13
14 | # File 'opal/stdlib/ostruct.rb', line 12
def [](name)
  @table[name.to_sym]
end | 
 
    
      
  
  
    #[]=(name, value)  ⇒ Object 
  
  
  
  
    | 
16
17
18 | # File 'opal/stdlib/ostruct.rb', line 16
def []=(name, value)
  @table[new_ostruct_member(name)] = value
end | 
 
    
      
  
  
    #delete_field(name)  ⇒ Object 
  
  
  
  
    | 
74
75
76
77
78
79
80
81 | # File 'opal/stdlib/ostruct.rb', line 74
def delete_field(name)
  sym = name.to_sym
  begin
    singleton_class.__send__(:remove_method, sym, "#{sym}=")
  rescue NameError
  end
  @table.delete sym
end | 
 
    
      
  
  
    #each_pair  ⇒ Object 
  
  
  
  
    | 
34
35
36
37
38
39
40 | # File 'opal/stdlib/ostruct.rb', line 34
def each_pair
  return enum_for :each_pair unless block_given?
  @table.each_pair do |pair|
    yield pair
  end
end | 
 
    
      
  
  
    #eql?(other)  ⇒ Boolean 
  
  
  
  
    | 
54
55
56
57
58 | # File 'opal/stdlib/ostruct.rb', line 54
def eql?(other)
  return false unless other.is_a?(OpenStruct)
  @table.eql? other.instance_variable_get(:@table)
end | 
 
    
      
  
  
    | 
68
69
70 | # File 'opal/stdlib/ostruct.rb', line 68
def hash
  @table.hash
end | 
 
    
      
  
  
    #inspect  ⇒ Object 
  
  
    Also known as:
    to_s
    
  
  
  
    | 
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 | # File 'opal/stdlib/ostruct.rb', line 94
def inspect
  %x{
    var top = (ostruct_ids === undefined),
        ostruct_id = #{__id__};
  }
  begin
    result = "#<#{self.class}"
    %x{
      if (top) {
        ostruct_ids = {};
      }
      if (ostruct_ids.hasOwnProperty(ostruct_id)) {
        return result + ' ...>';
      }
      ostruct_ids[ostruct_id] = true;
    }
    result += ' ' if @table.any?
    result += each_pair.map do |name, value|
      "#{name}=#{value.inspect}"
    end.join ', '
    result += '>'
    result
  ensure
    %x{
      if (top) {
        ostruct_ids = undefined;
      }
    }
  end
end | 
 
    
      
  
  
    #new_ostruct_member(name)  ⇒ Object 
  
  
  
  
    | 
83
84
85
86
87
88
89
90 | # File 'opal/stdlib/ostruct.rb', line 83
def new_ostruct_member(name)
  name = name.to_sym
  unless respond_to?(name)
    define_singleton_method(name) { @table[name] }
    define_singleton_method("#{name}=") { |x| @table[name] = x }
  end
  name
end | 
 
    
      
  
  
    | 
60
61
62 | # File 'opal/stdlib/ostruct.rb', line 60
def to_h
  @table.dup
end | 
 
    
      
  
  
    | 
64
65
66 | # File 'opal/stdlib/ostruct.rb', line 64
def to_n
  @table.to_n
end |