Class: Frontier::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/frontier/adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rx, tx, cache = {}) ⇒ Adapter

Returns a new instance of Adapter.



32
33
34
35
36
37
# File 'lib/frontier/adapter.rb', line 32

def initialize(rx, tx, cache = {})
  while true do
    reply = Adapter.process(self, rx, tx)
    cache[reply.object_id] = reply if reply
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



45
46
47
# File 'lib/frontier/adapter.rb', line 45

def method_missing(name, *args)
  %x[#{name} #{args.join(' ')}]
end

Class Method Details

.process(object, rx, tx) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/frontier/adapter.rb', line 19

def self.process(object, rx, tx)
  req = Marshal.load(rx)
  object = ObjectSpace._id2ref(req[:object]) if req.key?(:object)
  reply = object.send(req[:name], *req[:args])
  Marshal.dump(Adapter.wrap(reply), tx)
rescue Exception => e
  Marshal.dump(e, tx)
ensure
  tx.flush
  return reply
end

.wrap(object) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/frontier/adapter.rb', line 6

def self.wrap(object)
  case object
  when NilClass, TrueClass, FalseClass, Symbol, Numeric, String, Exception
    return object
  when Array
    return object.map { |o| wrap(o) }
  when Hash
    return object.merge(object) { |k, v| wrap(v) }
  else
    return Proxy.new(object)
  end
end

Instance Method Details

#load(*mixins) ⇒ Object



39
40
41
42
43
# File 'lib/frontier/adapter.rb', line 39

def load(*mixins)
  [*mixins].each do |mixin|
    self.class.class_eval("include Frontier::Mixin::#{mixin.capitalize}")
  end
end