1
0
Fork 0
mirror of https://github.com/rubyjs/therubyrhino synced 2023-03-27 23:21:34 -04:00
therubyrhino/lib/rhino/ruby/attribute_access.rb
kares 721c8a4355 some oop into access impls
AccessBase as a base class for different implementations
change DefaultAccess and RubyAccess to use AccessBase (but keep backwards compatibility with the "module" way)
2012-04-20 20:24:43 +02:00

41 lines
No EOL
1.2 KiB
Ruby

module Rhino
module Ruby
class AttributeAccess < AccessBase
def 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
super
end
def 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}" } )
return Rhino.to_javascript(method.call, scope)
else
return Function.wrap(method.unbind)
end
elsif object.respond_to?(:"#{name}=")
return nil # it does have the property but is non readable
end
super
end
def put(object, name, value)
if object.respond_to?(set_name = :"#{name}=")
return object.send(set_name, Rhino.to_ruby(value))
end
super
end
extend DeprecatedAccess # backward compatibility for a while
end
end
end