Module: Rhino::Ruby::AttributeAccess

Defined in:
lib/rhino/ruby/attribute_access.rb

Class Method Summary collapse

Class Method Details

.get(object, name, scope) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rhino/ruby/attribute_access.rb', line 17

def self.get(object, name, scope)
  name_sym = name.to_s.to_sym
  if object.respond_to?(name_sym)
    method = object.method(name_sym)
    if method.arity == 0 && # check if it is an attr_reader
      ( object.respond_to?(:"#{name}=") || 
          object.instance_variables.find { |var| var.to_sym == :"@#{name}" } )
      begin
        return Rhino.to_javascript(method.call, scope)
      rescue => e
        raise Rhino::Ruby.wrap_error(e)
      end
    else
      return Function.wrap(method.unbind)
    end
  elsif object.respond_to?(:"#{name}=")
    return nil # it does have the property but is non readable
  end
  # try [](name) method :
  if object.respond_to?(:'[]') && object.method(:'[]').arity == 1
    if value = object[name]
      return Rhino.to_javascript(value, scope)
    end
  end
  yield
end

.has(object, name, scope) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rhino/ruby/attribute_access.rb', line 5

def self.has(object, name, scope)
  if object.respond_to?(name.to_s) || 
     object.respond_to?(:"#{name}=") # might have a writer but no reader
    return true
  end
  # try [](name) method :
  if object.respond_to?(:'[]') && object.method(:'[]').arity == 1
    return true if object[name]
  end
  yield
end

.put(object, name, value) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/rhino/ruby/attribute_access.rb', line 44

def self.put(object, name, value)
  if object.respond_to?(set_name = :"#{name}=")
    return object.send(set_name, Rhino.to_ruby(value))
  end
  # try []=(name, value) method :
  if object.respond_to?(:'[]=') && object.method(:'[]=').arity == 2
    return object[name] = Rhino.to_ruby(value)
  end
  yield
end