Class: JS::Proxy
- Inherits:
-
Object
show all
- Includes:
- Enumerable, Helpers
- Defined in:
- lib/js/proxy.rb
Overview
Provides Ruby-style access to the properties and methods of a JavaScript object.
Constant Summary
collapse
- IRREGULARS =
%w[html url uri].freeze
Instance Method Summary
collapse
Methods included from Helpers
#native_methods, #wrap_result
Constructor Details
#initialize(native) ⇒ Proxy
Returns a new instance of Proxy.
60
61
62
|
# File 'lib/js/proxy.rb', line 60
def initialize(native)
self.native = native
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
72
73
74
75
76
77
78
79
80
|
# File 'lib/js/proxy.rb', line 72
def method_missing(name, *args, &block)
setter = name.end_with?("=")
property = resolve_property_name(name, allow_missing: setter)
return super unless property
return write_property(property, args.first) if setter
read_property(property, args, block)
end
|
Instance Method Details
#[](index) ⇒ Object
101
102
103
|
# File 'lib/js/proxy.rb', line 101
def [](index)
wrap_result(`#{to_n}[#{index}]`)
end
|
#[]=(key, value) ⇒ Object
105
106
107
108
109
|
# File 'lib/js/proxy.rb', line 105
def []=(key, value)
converted = unwrap_result(value)
`#{to_n}[#{key}] = #{converted}`
value
end
|
#each(&block) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/js/proxy.rb', line 87
def each(&block)
return enum_for(:each) unless block
if iterable?
each_iterable(&block)
elsif array_like?
each_array_like(&block)
else
raise TypeError, "#{self.class} does not wrap an iterable or array-like object"
end
self
end
|
#length ⇒ Object
119
120
121
|
# File 'lib/js/proxy.rb', line 119
def length
`#{to_n}.length`
end
|
#native ⇒ Object
64
65
66
|
# File 'lib/js/proxy.rb', line 64
def native
Native(to_n)
end
|
#native=(value) ⇒ Object
68
69
70
|
# File 'lib/js/proxy.rb', line 68
def native=(value)
@native = Native.try_convert(value, value)
end
|
#respond_to_missing?(name, include_private = false) ⇒ Boolean
82
83
84
85
|
# File 'lib/js/proxy.rb', line 82
def respond_to_missing?(name, include_private = false)
setter = name.end_with?("=")
!!resolve_property_name(name, allow_missing: setter) || super
end
|
#to_n ⇒ Object
111
112
113
|
# File 'lib/js/proxy.rb', line 111
def to_n
@native
end
|
#to_str ⇒ Object
115
116
117
|
# File 'lib/js/proxy.rb', line 115
def to_str
`String(#{to_n})`
end
|