Class: Rapuncel::Proxy
- Inherits:
-
Object
- Object
- Rapuncel::Proxy
- Defined in:
- lib/rapuncel/proxy.rb
Constant Summary collapse
- PROXY_METHODS =
%w(tap inspect clone freeze dup class initialize to_s).freeze
- LOCKED_METHODS =
%w(method_missing object_id).freeze
- LOCKED_PATTERN =
/(\A__|\?\Z|!\Z)/.freeze
Class Method Summary collapse
- .define_proxy_method(name) ⇒ Object
-
.define_proxy_methods(*names) ⇒ Object
Predefine some proxy methods.
-
.new(client_or_configuration, interface = nil) ⇒ Object
Initialize a new Proxy object for a specific Client.
Instance Method Summary collapse
-
#__initialize__(client, interface) ⇒ Object
:nodoc:.
- #call!(name, *args) ⇒ Object
-
#respond_to?(name) ⇒ Boolean
:nodoc:.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (protected)
:nodoc:
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rapuncel/proxy.rb', line 80 def method_missing name, *args, &block #:nodoc: name = name.to_s if LOCKED_PATTERN.match name super name.to_sym, *args, &block else self.__class__.define_proxy_method name call! name, *args, &block end end |
Class Method Details
.define_proxy_method(name) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/rapuncel/proxy.rb', line 31 def define_proxy_method name class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} *args, &block call! '#{name}', *args, &block end RUBY end |
.define_proxy_methods(*names) ⇒ Object
Predefine some proxy methods.
40 41 42 43 44 |
# File 'lib/rapuncel/proxy.rb', line 40 def define_proxy_methods *names names.each do |name| define_proxy_method name end end |
.new(client_or_configuration, interface = nil) ⇒ Object
Initialize a new Proxy object for a specific Client. Alternatively you can pass a Hash containing configuration for a new Client, which will be created on-the-fly, but is directly not accessible. The second parameter specifies a specific interface/namespace for the remote calls, i.e. if your RPC method is
int numbers.add(int a, int b)
You can create a specific proxy for numbers
, and use add
directly
proxy = Proxy.new client, 'numbers'
proxy.add(40, 2) -> 42
23 24 25 26 27 28 29 |
# File 'lib/rapuncel/proxy.rb', line 23 def new client_or_configuration, interface = nil client_or_configuration = Client.new client_or_configuration if client_or_configuration.is_a?(Hash) allocate.__tap__ do |new_proxy| new_proxy.__initialize__ client_or_configuration, interface end end |
Instance Method Details
#__initialize__(client, interface) ⇒ Object
:nodoc:
70 71 72 73 |
# File 'lib/rapuncel/proxy.rb', line 70 def __initialize__ client, interface #:nodoc: @interface = interface @client = client end |
#call!(name, *args) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rapuncel/proxy.rb', line 59 def call! name, *args name = "#{@interface}.#{name}" if @interface @client.call_to_ruby(name, *args).tap do |response| if block_given? yield response end end end |
#respond_to?(name) ⇒ Boolean
:nodoc:
75 76 77 |
# File 'lib/rapuncel/proxy.rb', line 75 def respond_to? name #:nodoc: LOCKED_PATTERN.match(name.to_s) ? super : true end |