Class: Twostroke::Context::ObjectProxy

Inherits:
BasicObject
Defined in:
lib/twostroke/context/object_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ ObjectProxy

Returns a new instance of ObjectProxy.



4
5
6
# File 'lib/twostroke/context/object_proxy.rb', line 4

def initialize(object)
  @object = object
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(prop, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/twostroke/context/object_proxy.rb', line 19

def method_missing(prop, *args, &block)
  return self[prop] = args[0] if prop =~ /=\z/
  val = self[prop]
  if val.respond_to? :call
    val.call(*args)
  elsif args.size > 0
    ::Kernel.p args
    ::Kernel.raise "Cannot call non-callable"
  else
    val
  end
end

Instance Method Details

#[](prop) ⇒ Object



8
9
10
# File 'lib/twostroke/context/object_proxy.rb', line 8

def [](prop)
  o = @object.get(prop.to_s) and o.to_ruby
end

#[]=(prop, val) ⇒ Object



12
13
14
15
16
17
# File 'lib/twostroke/context/object_proxy.rb', line 12

def []=(prop, val)
  unless val.is_a? ::Twostroke::Runtime::Types::Value
    val = ::Twostroke::Runtime::Types.marshal val
  end
  @object.put prop.to_s, val
end

#inspectObject



32
33
34
35
36
37
38
39
# File 'lib/twostroke/context/object_proxy.rb', line 32

def inspect
  if toString = @object.get("toString") and toString.respond_to? :call
    s = toString.call(nil, @object, [])
    if s.is_a? ::Twostroke::Runtime::Types::Primitive
      ::Twostroke::Runtime::Types.to_string(s).string
    end
  end
end