Class: Rack::RPC::Service

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

Overview

Represents an RPC service.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- method_missing(method_name, *args, &block) (protected)

This method returns an undefined value.

Parameters:

  • method_name (Symbol, #to_sym)
  • args (Array)

Raises:

  • (NoMethodError)

    if self doesn't respond to method_name



58
59
60
61
62
63
64
# File 'lib/rack/rpc/service.rb', line 58

def method_missing(method_name, *args, &block)
  if (operator = self.class[method_name]).nil?
    super # raises NoMethodError
  else
    operator.new(args).execute
  end
end

Class Method Details

+ (Class) [](operator_name)

Returns the operator class for the given operator name.

Parameters:

  • operator_name (Symbol, #to_sym)

Returns:

  • (Class)


38
39
40
41
42
43
44
# File 'lib/rack/rpc/service.rb', line 38

def self.[](operator_name)
  operator_name = operator_name.to_sym
  operators.find do |klass, options|
    klass_name = klass.name.split('::').last # TODO: optimize this
    return klass if operator_name.eql?(klass_name.to_sym)
  end
end

+ operator(klass, options = {})

This method returns an undefined value.

Defines an operator for this service class.

Examples:

class Calculator < Service
  operator Add
  operator Subtract
  operator Multiply
  operator Divide
end

Parameters:

  • klass (Class)
  • options (Hash{Symbol => Object}) (defaults to: {})

Raises:

  • (TypeError)


20
21
22
23
# File 'lib/rack/rpc/service.rb', line 20

def self.operator(klass, options = {})
  raise TypeError, "expected a Class, but got #{klass.inspect}" unless klass.is_a?(Class)
  operators[klass] ||= options
end

+ (Hash{Class => Hash}) operators

Returns the operator definitions for this service class.

Returns:

  • (Hash{Class => Hash})


29
30
31
# File 'lib/rack/rpc/service.rb', line 29

def self.operators
  @operators ||= {}
end

Instance Method Details

- (Boolean) respond_to?(method_name)

Returns true or false

Parameters:

  • method_name (Symbol, #to_sym)

Returns:

  • (Boolean)

    true or false



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

def respond_to?(method_name)
  super || (self.class[method_name] ? true : false)
end