class Culerity::RemoteObjectProxy

Public Class Methods

new(remote_object_id, io) click to toggle source
# File lib/culerity/remote_object_proxy.rb, line 11
def initialize(remote_object_id, io)
  @remote_object_id = remote_object_id
  @io = io
end

Public Instance Methods

exit() click to toggle source
# File lib/culerity/remote_object_proxy.rb, line 51
def exit
  @io << '["_exit_"]'
end
id() click to toggle source

Commonly used to get the HTML id attribute Use ‘object_id` to get the local objects’ id.

# File lib/culerity/remote_object_proxy.rb, line 20
def id
  send_remote(:id)
end
inspect() click to toggle source
# File lib/culerity/remote_object_proxy.rb, line 24
def inspect
  send_remote(:inspect)
end
method_missing(name, *args, &block) click to toggle source
# File lib/culerity/remote_object_proxy.rb, line 32
def method_missing(name, *args, &block)
  send_remote(name, *args, &block)
end
respond_to?(name) click to toggle source
# File lib/culerity/remote_object_proxy.rb, line 28
def respond_to?(name)
  send_remote :respond_to?, name
end
send_remote(name, *args, &blk) click to toggle source

Calls the passed method on the remote object with any arguments specified. Behaves the same as Object#send.

If you pass it a block then it will append the block as a “lambda { … }”. If your block returns a lambda string (“lambda { … }”) then it will be passed straight through, otherwise it will be wrapped in a lambda string before sending.

# File lib/culerity/remote_object_proxy.rb, line 44
def send_remote(name, *args, &blk)
  input = [remote_object_id, %Q{"#{name}"}, *args.map{|a| arg_to_string(a)}]
  serialized_block = ", #{block_to_string(&blk)}" if block_given?
  @io << "[[#{input.join(", ")}]#{serialized_block}]\n"
  process_result @io.gets.to_s.strip
end

Private Instance Methods

arg_to_string(arg) click to toggle source
# File lib/culerity/remote_object_proxy.rb, line 84
def arg_to_string(arg)
  if arg.is_a?(Proc)
    block_to_string(&arg)
  else
    arg.inspect
  end
end
block_to_string(&block) click to toggle source

Takes a block and either returns the result (if it returns “lambda { … }”) or builds the lambda string with the result of the block in it.

Returns a string in the format “lambda { … }”

# File lib/culerity/remote_object_proxy.rb, line 76
def block_to_string &block
  result = block.call.to_s.strip
  unless result.is_a?(String) && result[/^lambda\s*(\{|do).+(\}|end)/xm]
    result = "lambda { #{result} }"
  end
  result.gsub("\n", ";")
end
process_result(result) click to toggle source
# File lib/culerity/remote_object_proxy.rb, line 57
def process_result(result)
  res = eval result
  if res.first == :return
    res[1]
  elsif res.first == :exception
    begin
      raise "local trace"
    rescue => ex
      raise CulerityException.new("#{res[1]}: #{res[2]}", res[3] + ex.backtrace)
    end
  end
end
remote_object_id() click to toggle source
# File lib/culerity/remote_object_proxy.rb, line 92
def remote_object_id
  @remote_object_id
end