Class: Stargate::Client::Proxy
- Inherits:
-
Object
- Object
- Stargate::Client::Proxy
- 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
-
.configure_stargate_portal(protocol, metadata) ⇒ Object
Internal: Configures portal for given protocol.
-
.define_attributes(*names) ⇒ Object
Internal: Helper to define all attributres (accessors).
-
.define_readers(*names) ⇒ Object
Internal: Helper to define reader methods.
-
.metaclass ⇒ Object
Internal: Magic for metaprogramming tricks.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Proxy
constructor
Public: Constructor.
- #to_h ⇒ Object
Constructor Details
permalink #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.
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
permalink .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.
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, ) .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 |
permalink .define_attributes(*names) ⇒ Object
Internal: Helper to define all attributres (accessors).
33 34 35 |
# File 'lib/stargate/client/proxy.rb', line 33 def self.define_attributes(*names) attr_accessor(*names) end |
permalink .define_readers(*names) ⇒ Object
Internal: Helper to define reader methods.
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 |
permalink .metaclass ⇒ Object
Internal: Magic for metaprogramming tricks.
9 10 11 |
# File 'lib/stargate/client/proxy.rb', line 9 def self. class << self; self; end end |
Instance Method Details
permalink #to_h ⇒ Object
[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 |