Class: Bridge::Reference

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

Overview

Instances of this class represent references to remote services.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bridge, address, operations = nil) ⇒ Reference

Returns a new instance of Reference.



7
8
9
10
11
12
13
14
15
# File 'lib/reference.rb', line 7

def initialize bridge, address, operations = nil
  @address = address
  # Store operations supported by this reference if any
  operations = [] if operations.nil?
  @operations = operations.map do |val|
    val.to_s
  end
  @bridge = bridge
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(atom, *args, &blk) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/reference.rb', line 34

def method_missing atom, *args, &blk
  # If a block is given, add to arguments list
  args << blk if blk
  Util.info "Calling #{@address}.#{atom}"
  # Serialize destination
  destination = self.to_dict atom.to_s
  # Send RPC
  @bridge.send args, destination
end

Instance Attribute Details

#addressObject

:nodoc: all



5
6
7
# File 'lib/reference.rb', line 5

def address
  @address
end

#operationsObject

:nodoc: all



5
6
7
# File 'lib/reference.rb', line 5

def operations
  @operations
end

Instance Method Details

#respond_to?(atom) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/reference.rb', line 44

def respond_to? atom
  @operations.include?(atom.to_s) || atom == :to_dict || Class.respond_to?(atom)
end

#to_dict(op = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/reference.rb', line 17

def to_dict op = nil
  # Serialize the reference
  result = {}
  address = @address
  # Add a method name to address if given
  if op
    address = address.slice(0..-1)
    address << op
  end
  result[:ref] = address
  # Append operations only if address refers to a handler
  if address.length < 4
    result[:operations] = @operations
  end
  return result
end