Class: Stargate::Client::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/stargate/client/proxy.rb

Overview

Internal: Here’s where the magic happens. The proxy class connects local method calls with remote endpoints. Basically whenever any of registered (provided by remote definitions) methods is called, proxy forwards this call to remote location and returns obtained results adapted to local context.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Proxy

Public: Constructor. Proxy class ignores remote constructor definition. Local proxy is always treaded as a data structure only and thus it’s convenient to initialize it with attributes to set.

[View source]

50
51
52
53
54
# File 'lib/stargate/client/proxy.rb', line 50

def initialize(attributes = {})
  attributes.each do |name,value|
    instance_variable_set("@#{name}", value)
  end
end

Class Method Details

.configure_stargate_portal(protocol, metadata) ⇒ Object

Internal: Configures portal for given protocol. Takes class metadata to define all entry point class methods, object attributes and readers.

[View source]

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stargate/client/proxy.rb', line 15

def self.configure_stargate_portal(protocol, )
  metaclass.instance_eval do
    .class_methods.each do |method|
      define_method(method) do |*args|
        protocol.call(self, method, *args)
      end
    end

    define_method(:remote_name) do
      .name
    end
  end

  define_attributes(*.attributes)
  define_readers(*.readers)
end

.define_attributes(*names) ⇒ Object

Internal: Helper to define all attributres (accessors).

[View source]

33
34
35
# File 'lib/stargate/client/proxy.rb', line 33

def self.define_attributes(*names)
  attr_accessor(*names)
end

.define_readers(*names) ⇒ Object

Internal: Helper to define reader methods.

[View source]

38
39
40
41
42
43
44
45
# File 'lib/stargate/client/proxy.rb', line 38

def self.define_readers(*names)
  names.each do |name|
    name = name.to_s
    simple_name = name.split('?').first
    attr_reader(simple_name)
    alias name simple_name if simple_name != name
  end
end

.metaclassObject

Internal: Magic for metaprogramming tricks.

[View source]

9
10
11
# File 'lib/stargate/client/proxy.rb', line 9

def self.metaclass
  class << self; self; end
end

Instance Method Details

#to_hObject

[View source]

56
57
58
59
60
61
# File 'lib/stargate/client/proxy.rb', line 56

def to_h
  self.class.stargate_directives.attributes.inject({}) do |hash,name|
    hash[name] = instance_variable_get("@#{name}")
    hash
  end
end