class Stargate::Client::Proxy

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.

Public Class Methods

configure_stargate_portal(protocol, metadata) click to toggle source

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

# File lib/stargate/client/proxy.rb, line 15
def self.configure_stargate_portal(protocol, metadata)
  metaclass.instance_eval do
    metadata.class_methods.each do |method|
      define_method(method) do |*args|
        protocol.call(self, method, *args)
      end
    end

    define_method(:remote_name) do
      metadata.name
    end
  end

  define_attributes(*metadata.attributes)
  define_readers(*metadata.readers)
end
define_attributes(*names) click to toggle source

Internal: Helper to define all attributres (accessors).

# File lib/stargate/client/proxy.rb, line 33
def self.define_attributes(*names)
  attr_accessor(*names)
end
define_readers(*names) click to toggle source

Internal: Helper to define reader methods.

# 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
metaclass() click to toggle source

Internal: Magic for metaprogramming tricks.

# File lib/stargate/client/proxy.rb, line 9
def self.metaclass
  class << self; self; end
end
new(attributes = {}) click to toggle source

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.

# File lib/stargate/client/proxy.rb, line 50
def initialize(attributes = {})
  attributes.each do |name,value|
    instance_variable_set("@#{name}", value)
  end
end

Public Instance Methods

to_h() click to toggle source
# 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