Class: JsObject

Inherits:
Prototype show all
Defined in:
lib/js_object.rb

Instance Method Summary collapse

Constructor Details

#initialize(prototype = nil) ⇒ JsObject

Returns a new instance of JsObject.



5
6
7
8
9
# File 'lib/js_object.rb', line 5

def initialize(prototype=nil)
  self.nil_keys = []
  self.false_keys = []
  self[:prototype] = prototype || PROTOTYPE
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object (private)



49
50
51
52
# File 'lib/js_object.rb', line 49

def method_missing(method, *arguments, &block)
  return super if equals_method?(method) || (block && prototype[method].nil?)
  delegate_to_prototype method, arguments, block
end

Instance Method Details

#[](key) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/js_object.rb', line 11

def [](key)
  if nil_keys.include? key.to_s
    nil
  elsif false_keys.include? key.to_s
    false
  else
    super || prototype[key]
  end
end

#[]=(key, value) ⇒ Object



21
22
23
24
25
# File 'lib/js_object.rb', line 21

def []=(key, value)
  remove_from_falsey_lists key.to_s
  add_to_falsey_lists key.to_s, value
  super
end

#delete(property) ⇒ Object



27
28
29
30
# File 'lib/js_object.rb', line 27

def delete(property)
  remove_from_falsey_lists property.to_s
  super
end