Class: OpenStruct
Overview
backtick_javascript: true
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = nil) ⇒ OpenStruct
Returns a new instance of OpenStruct.
4
5
6
7
8
9
10
11
12
|
# File 'opal/stdlib/ostruct.rb', line 4
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
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'opal/stdlib/ostruct.rb', line 22
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.
79
80
81
|
# File 'opal/stdlib/ostruct.rb', line 79
def table
@table
end
|
Instance Method Details
#==(other) ⇒ Object
49
50
51
52
53
|
# File 'opal/stdlib/ostruct.rb', line 49
def ==(other)
return false unless other.is_a?(OpenStruct)
@table == other.instance_variable_get(:@table)
end
|
#===(other) ⇒ Object
55
56
57
58
59
|
# File 'opal/stdlib/ostruct.rb', line 55
def ===(other)
return false unless other.is_a?(OpenStruct)
@table === other.instance_variable_get(:@table)
end
|
14
15
16
|
# File 'opal/stdlib/ostruct.rb', line 14
def [](name)
@table[name.to_sym]
end
|
#[]=(name, value) ⇒ Object
18
19
20
|
# File 'opal/stdlib/ostruct.rb', line 18
def []=(name, value)
@table[new_ostruct_member(name)] = value
end
|
#delete_field(name) ⇒ Object
81
82
83
84
85
86
87
88
|
# File 'opal/stdlib/ostruct.rb', line 81
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
41
42
43
44
45
46
47
|
# File 'opal/stdlib/ostruct.rb', line 41
def each_pair
return enum_for :each_pair unless block_given?
@table.each_pair do |pair|
yield pair
end
end
|
#eql?(other) ⇒ Boolean
61
62
63
64
65
|
# File 'opal/stdlib/ostruct.rb', line 61
def eql?(other)
return false unless other.is_a?(OpenStruct)
@table.eql? other.instance_variable_get(:@table)
end
|
75
76
77
|
# File 'opal/stdlib/ostruct.rb', line 75
def hash
@table.hash
end
|
#inspect ⇒ Object
Also known as:
to_s
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
128
129
130
131
132
133
134
|
# File 'opal/stdlib/ostruct.rb', line 101
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
90
91
92
93
94
95
96
97
|
# File 'opal/stdlib/ostruct.rb', line 90
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
|
#respond_to_missing?(mid, include_private = false) ⇒ Boolean
36
37
38
39
|
# File 'opal/stdlib/ostruct.rb', line 36
def respond_to_missing?(mid, include_private = false) mname = mid.to_s.chomp('=').to_sym
@table&.key?(mname) || super
end
|
#to_h(&block) ⇒ Object
67
68
69
|
# File 'opal/stdlib/ostruct.rb', line 67
def to_h(&block)
block_given? ? @table.to_h(&block) : @table.dup
end
|
71
72
73
|
# File 'opal/stdlib/ostruct.rb', line 71
def to_n
@table.to_n
end
|