Class: Rack::RPC::Operation

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

Overview

Represents an RPC server operation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ Operation

Initializes a new operation with the given arguments.

Parameters:

  • args (Hash{Symbol => Object}) (defaults to: [])


103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rack/rpc/operation.rb', line 103

def initialize(args = [])
  case args
    when Array then initialize_from_array(args)
    when Hash  then initialize_from_hash(args)
    else case
      when args.respond_to?(:to_args)
        initialize_from_array(args.to_args)
        @__context__ = args.context if args.respond_to?(:context)
      else raise ArgumentError, "expected an Array or Hash, but got #{args.inspect}"
    end
  end

  initialize! if respond_to?(:initialize!)
end

Instance Attribute Details

#contextObject (readonly)

Returns:

  • (Object)


96
97
98
# File 'lib/rack/rpc/operation.rb', line 96

def context
  @context
end

Class Method Details

.arityRange

Returns the arity range for this operation class.

Returns:

  • (Range)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rack/rpc/operation.rb', line 79

def self.arity
  @arity ||= begin
    if const_defined?(:ARITY)
      const_get(:ARITY)
    else
      min, max = 0, 0
      operands.each do |name, options|
        min += 1 unless options[:optional].eql?(true)
        max += 1
      end
      Range.new(min, max)
    end
  end
end

.execute { ... } ⇒ void

This method returns an undefined value.

Defines the ‘#execute` instance method.

Yields:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rack/rpc/operation.rb', line 53

def self.execute(&block)
  self.send(:define_method, :execute) do
    begin
      before_execute if respond_to?(:before_execute)
      result = instance_eval(&block)
      after_execute if respond_to?(:after_execute)
      result
    rescue Exception => error
      after_error(error) if respond_to?(:after_error)
      raise
    end
  end
end

.operand(name, type = Object, options = {}) ⇒ void

This method returns an undefined value.

Defines an operand for this operation class.

Examples:

class Multiply < Operation
  operand :x, Numeric
  operand :y, Numeric
end

Parameters:

  • name (Symbol, #to_sym)
  • type (Class) (defaults to: Object)
  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :optional (Boolean) — default: false
  • :nullable (Boolean) — default: false

Raises:

  • (TypeError)


21
22
23
24
# File 'lib/rack/rpc/operation.rb', line 21

def self.operand(name, type = Object, options = {})
  raise TypeError, "expected a Class, but got #{type.inspect}" unless type.is_a?(Class)
  operands[name.to_sym] = options.merge(:type => type)
end

.operandsHash{Symbol => Hash}

Returns the operand definitions for this operation class.

Returns:

  • (Hash{Symbol => Hash})


71
72
73
# File 'lib/rack/rpc/operation.rb', line 71

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

.prepare { ... } ⇒ void

This method returns an undefined value.

Defines the ‘#prepare` instance method.

Yields:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/rpc/operation.rb', line 31

def self.prepare(&block)
  self.send(:define_method, :prepare) do
    begin
      begin
        before_prepare if respond_to?(:before_prepare)
        instance_eval(&block)
      ensure
        after_prepare if respond_to?(:after_prepare)
      end
      self
    rescue Exception => error
      after_error(error) if respond_to?(:after_error)
      raise
    end
  end
end

Instance Method Details

#executevoid

This method is abstract.

This method returns an undefined value.

Executes this operation.

Raises:

  • (NotImplementedError)


192
193
194
# File 'lib/rack/rpc/operation.rb', line 192

def execute
  raise NotImplementedError, "#{self.class}#execute"
end

#preparevoid

This method is abstract.

This method returns an undefined value.

Prepares this operation.



183
184
185
# File 'lib/rack/rpc/operation.rb', line 183

def prepare
  self
end

#to_aArray

Returns the array representation of the arguments to this operation.

Returns:

  • (Array)


200
201
202
203
204
205
# File 'lib/rack/rpc/operation.rb', line 200

def to_a
  self.class.operands.inject([]) do |result, (param_name, param_options)|
    result << instance_variable_get("@#{param_name}")
    result
  end
end

#to_hashHash

Returns the hash representation of the arguments to this operation.

Returns:

  • (Hash)


211
212
213
214
215
216
# File 'lib/rack/rpc/operation.rb', line 211

def to_hash
  self.class.operands.inject({}) do |result, (param_name, param_options)|
    result[param_name] = instance_variable_get("@#{param_name}")
    result
  end
end

#to_jsonString

Returns the JSON representation of the arguments to this operation.

Returns:

  • (String)

    a serialized JSON object



222
223
224
# File 'lib/rack/rpc/operation.rb', line 222

def to_json
  to_hash.to_json
end