Class: Native::Object

Inherits:
Object show all
Includes:
Enumerable, Native
Defined in:
lib/opal/native.rb

Instance Method Summary collapse

Methods included from Native

===, included, #native_send, send, #to_native

Constructor Details

#initializeObject

Returns a new instance of Object.



62
63
64
65
66
# File 'lib/opal/native.rb', line 62

def initialize (*)
	super

	update!
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
# File 'lib/opal/native.rb', line 68

def == (other)
	`#@native == #{Native(other).to_native}`
end

#===(other) ⇒ Object



72
73
74
# File 'lib/opal/native.rb', line 72

def === (other)
	Native::Object === other && `#@native == #{other.to_native}`
end

#[](name) ⇒ Object



76
77
78
# File 'lib/opal/native.rb', line 76

def [] (name)
	Kernel.Native(`#@native[name]`)
end

#[]=(name, value) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/opal/native.rb', line 80

def []= (name, value)
	value = value.to_native unless Native === value

	`#@native[name] = #{value}`

	update!(name)

	value
end

#eachObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/opal/native.rb', line 90

def each
	return enum_for :each unless block_given?

	%x{
		for (var name in #@native) {
			#{yield Kernel.Native(`name`), Kernel.Native(`#@native[name]`)}
		}
	}

	self
end

#each_keyObject



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/opal/native.rb', line 102

def each_key
	return enum_for :each_key unless block_given?

	%x{
		for (var name in #@native) {
			#{yield Kernel.Native(`name`)}
		}
	}

	self
end

#each_valueObject



114
115
116
117
118
119
120
121
122
# File 'lib/opal/native.rb', line 114

def each_value
	return enum_for :each_value unless block_given?

	%x{
		for (var name in #@native) {
			#{yield Kernel.Native(`#@native[name]`)}
		}
	}
end

#inspectObject



124
125
126
# File 'lib/opal/native.rb', line 124

def inspect
	"#<Native: #{`#@native.toString()`}>"
end

#keysObject



128
129
130
# File 'lib/opal/native.rb', line 128

def keys
	each_key.to_a
end

#nil?Boolean

Returns:



132
133
134
# File 'lib/opal/native.rb', line 132

def nil?
	`#@native === null || #@native === undefined`
end

#to_hashObject



140
141
142
# File 'lib/opal/native.rb', line 140

def to_hash
	Hash[to_a]
end

#to_sObject



136
137
138
# File 'lib/opal/native.rb', line 136

def to_s
	`#@native.toString()`
end

#update!(name = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/opal/native.rb', line 148

def update! (name = nil)
	unless name
		%x{
			for (var name in #@native) {
				#{update!(`name`)}
			}
		}

		return
	end

	if Proc === `#@native[name]`
		define_singleton_method name do |*args, &block|
			__native_send__(name, *args, &block)
		end

		if respond_to? "#{name}="
			singleton_class.undef_method "#{name}="
		end
	else
		define_singleton_method name do
			self[name]
		end

		define_singleton_method "#{name}=" do |value|
			self[name] = value
		end
	end
end

#valuesObject



144
145
146
# File 'lib/opal/native.rb', line 144

def values
	each_value.to_a
end