[フレーム]

Class: OpenStruct

Inherits:
Object show all
Defined in:
opal/stdlib/ostruct.rb

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
# File 'opal/stdlib/ostruct.rb', line 2
def initialize(hash = nil)
 @table = {}
 hash.each_pair {|key, value|
 @table[new_ostruct_member(key)] = value
 } if hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'opal/stdlib/ostruct.rb', line 18
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.new "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

#tableObject (readonly)

Returns the value of attribute table

70
71
72
# File 'opal/stdlib/ostruct.rb', line 70
def table
 @table
end

Instance Method Details

#==(other) ⇒ Object

40
41
42
43
44
# File 'opal/stdlib/ostruct.rb', line 40
def ==(other)
 return false unless other.is_a?(OpenStruct )
 @table == other.instance_variable_get(:@table)
end

#===(other) ⇒ Object

46
47
48
49
50
# File 'opal/stdlib/ostruct.rb', line 46
def ===(other)
 return false unless other.is_a?(OpenStruct )
 @table === other.instance_variable_get(:@table)
end

#[](name) ⇒ Object

10
11
12
# File 'opal/stdlib/ostruct.rb', line 10
def [](name)
 @table[name.to_sym]
end

#[]=(name, value) ⇒ Object

14
15
16
# File 'opal/stdlib/ostruct.rb', line 14
def []=(name, value)
 @table[new_ostruct_member(name)] = value
end

#delete_field(name) ⇒ Object

72
73
74
75
76
77
78
79
# File 'opal/stdlib/ostruct.rb', line 72
def delete_field(name)
 sym = name.to_sym
 begin
 singleton_class.__send__(:remove_method, sym, "#{sym}=")
 rescue NameError
 end
 @table.delete sym
end

#each_pairObject

32
33
34
35
36
37
38
# File 'opal/stdlib/ostruct.rb', line 32
def each_pair
 return enum_for :each_pair unless block_given?
 @table.each_pair {|pair|
 yield pair
 }
end

#eql?(other) ⇒ Boolean

Returns:

52
53
54
55
56
# File 'opal/stdlib/ostruct.rb', line 52
def eql?(other)
 return false unless other.is_a?(OpenStruct )
 @table.eql? other.instance_variable_get(:@table)
end

#hashObject

66
67
68
# File 'opal/stdlib/ostruct.rb', line 66
def hash
 @table.hash
end

#inspectObject Also known as: to_s

92
93
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
# File 'opal/stdlib/ostruct.rb', line 92
def inspect
 %x{
 var top = (ostruct_ids === undefined),
 ostruct_id = #{self.__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 {|name, value|
 "#{name}=#{value.inspect}"
 }.join ", "
 result += ">"
 result 
 ensure
 %x{
 if (top) {
 ostruct_ids = undefined;
 }
 }
 end
end

#new_ostruct_member(name) ⇒ Object

81
82
83
84
85
86
87
88
# File 'opal/stdlib/ostruct.rb', line 81
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

#to_hObject

58
59
60
# File 'opal/stdlib/ostruct.rb', line 58
def to_h
 @table.dup
end

#to_nObject

62
63
64
# File 'opal/stdlib/ostruct.rb', line 62
def to_n
 @table.to_n
end

AltStyle によって変換されたページ (->オリジナル) /