2012-01-07 11:01:38 -05:00
|
|
|
module Rhino
|
|
|
|
module Ruby
|
2012-04-20 14:24:43 -04:00
|
|
|
class AttributeAccess < AccessBase
|
2012-01-07 11:01:38 -05:00
|
|
|
|
2012-04-20 14:24:43 -04:00
|
|
|
def has(object, name, scope)
|
2012-01-07 11:01:38 -05:00
|
|
|
if object.respond_to?(name.to_s) ||
|
2012-02-15 06:36:16 -05:00
|
|
|
object.respond_to?(:"#{name}=") # might have a writer but no reader
|
2012-01-07 11:01:38 -05:00
|
|
|
return true
|
|
|
|
end
|
2012-04-20 14:24:43 -04:00
|
|
|
super
|
2012-01-07 11:01:38 -05:00
|
|
|
end
|
|
|
|
|
2012-04-20 14:24:43 -04:00
|
|
|
def get(object, name, scope)
|
2012-02-15 06:36:16 -05:00
|
|
|
name_sym = name.to_s.to_sym
|
|
|
|
if object.respond_to?(name_sym)
|
|
|
|
method = object.method(name_sym)
|
2012-01-07 11:01:38 -05:00
|
|
|
if method.arity == 0 && # check if it is an attr_reader
|
2012-02-15 06:36:16 -05:00
|
|
|
( object.respond_to?(:"#{name}=") ||
|
|
|
|
object.instance_variables.find { |var| var.to_sym == :"@#{name}" } )
|
2012-04-20 14:24:43 -04:00
|
|
|
return Rhino.to_javascript(method.call, scope)
|
2012-01-07 11:01:38 -05:00
|
|
|
else
|
2012-01-10 11:24:12 -05:00
|
|
|
return Function.wrap(method.unbind)
|
2012-01-07 11:01:38 -05:00
|
|
|
end
|
2012-02-15 06:36:16 -05:00
|
|
|
elsif object.respond_to?(:"#{name}=")
|
2012-01-07 11:01:38 -05:00
|
|
|
return nil # it does have the property but is non readable
|
|
|
|
end
|
2012-04-20 14:24:43 -04:00
|
|
|
super
|
2012-01-07 11:01:38 -05:00
|
|
|
end
|
|
|
|
|
2012-04-20 14:24:43 -04:00
|
|
|
def put(object, name, value)
|
2012-02-15 06:36:16 -05:00
|
|
|
if object.respond_to?(set_name = :"#{name}=")
|
2012-01-07 11:01:38 -05:00
|
|
|
return object.send(set_name, Rhino.to_ruby(value))
|
|
|
|
end
|
2012-04-20 14:24:43 -04:00
|
|
|
super
|
2012-01-07 11:01:38 -05:00
|
|
|
end
|
|
|
|
|
2012-04-20 14:24:43 -04:00
|
|
|
extend DeprecatedAccess # backward compatibility for a while
|
|
|
|
|
2012-01-07 11:01:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|