class Rack::RPC::Server

A base class for RPC servers.

Attributes

options[R]

@return [Hash]

request[RW]

@return [Rack::Request]

Public Class Methods

[](rpc_method_name) click to toggle source

@private

# 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) click to toggle source
# 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) click to toggle source
# 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
hooks() click to toggle source
# File lib/rack/rpc/server.rb, line 36
def self.hooks
  @hooks ||= {:before => [], :after => []}
end
new(options = {}, &block) click to toggle source

@param [Hash] options

# File lib/rack/rpc/server.rb, line 56
def initialize(options = {}, &block)
  @options = options.dup
  block.call(self) if block_given?
end
rpc(mappings = {}) click to toggle source

@private

# 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

Private Class Methods

setup_hook(type, method, options, proc) click to toggle source
# File lib/rack/rpc/server.rb, line 63
def self.setup_hook(type, method, options, proc)
  hooks[type] << if proc
    ProcCommand.new(proc, options)
  else
    MethodCommand.new(method, options)
  end
end