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: {})


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

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

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)


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

def options
  @options
end

#requestRack::Request

Returns:

  • (Rack::Request)


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

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



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

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



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

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

.hooksObject



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

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
34
# 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)}
        method = :"#{server_method}_without_callbacks"
        out = args.any? ? self.send(method, *args) : self.send(method)
        self.class.hooks[:after].each{|command| command.call(self) if command.callable?(server_method)}
        out
      end
    end
  end
end