Class: Rack::RPC::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/rpc/server.rb

Overview

A base class for RPC servers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Server

Returns a new instance of Server.

Parameters:

  • options (Hash) (defaults to: {})


55
56
57
58
# File 'lib/rack/rpc/server.rb', line 55

def initialize(options = {}, &block)
  @options = options.dup
  block.call(self) if block_given?
end

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)


48
49
50
# File 'lib/rack/rpc/server.rb', line 48

def options
  @options
end

#requestRack::Request

Returns:

  • (Rack::Request)


51
52
53
# File 'lib/rack/rpc/server.rb', line 51

def request
  @request
end

Class Method Details

.[](rpc_method_name) ⇒ Object



7
8
9
10
# File 'lib/rack/rpc/server.rb', line 7

def self.[](rpc_method_name)
  @mappings ||= {}
  @mappings[rpc_method_name]
end

.after_filter(method_sym = nil, options = {}, &block) ⇒ Object



43
44
45
# File 'lib/rack/rpc/server.rb', line 43

def self.after_filter(method_sym = nil, options = {}, &block)
  setup_hook(:after, method_sym, options, block)
end

.before_filter(method_sym = nil, options = {}, &block) ⇒ Object



39
40
41
# File 'lib/rack/rpc/server.rb', line 39

def self.before_filter(method_sym = nil, options = {}, &block)
  setup_hook(:before, method_sym, options, block)
end

.hooksObject



35
36
37
# File 'lib/rack/rpc/server.rb', line 35

def self.hooks
  @hooks ||= {:before => [], :after => []}
end

.rpc(mappings = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rack/rpc/server.rb', line 14

def self.rpc(mappings = {})
  @mappings ||= {}
  if mappings.empty?
    @mappings
  else
    # Store the mappings
    @mappings.merge!(mappings)

    # Wrap each method so we can inject before and after callbacks
    mappings.each do |rpc_method_name, server_method|
      self.send(:alias_method, :"#{server_method}_without_callbacks", server_method.to_sym)
      self.send(:define_method, server_method) do |*args|
        self.class.hooks[:before].each{|command| command.call(self) if command.callable?(server_method)}
        out = self.send(:"#{server_method}_without_callbacks", *args)
        self.class.hooks[:after].each{|command| command.call(self) if command.callable?(server_method)}
        out
      end
    end
  end
end