Class: Rack::RPC::Operation
- Inherits:
-
Object
- Object
- Rack::RPC::Operation
- Defined in:
- lib/rack/rpc/operation.rb
Overview
Represents an RPC server operation.
Instance Attribute Summary (collapse)
- - (Object) context readonly
Class Method Summary (collapse)
-
+ (Range) arity
Returns the arity range for this operation class.
-
+ execute { ... }
Defines the
#execute
instance method. -
+ operand(name, type = Object, options = {})
Defines an operand for this operation class.
-
+ (Hash{Symbol => Hash}) operands
Returns the operand definitions for this operation class.
-
+ prepare { ... }
Defines the
#prepare
instance method.
Instance Method Summary (collapse)
-
- execute
abstract
Executes this operation.
-
- (Operation) initialize(args = [])
constructor
Initializes a new operation with the given arguments.
-
- prepare
abstract
Prepares this operation.
-
- (Array) to_a
Returns the array representation of the arguments to this operation.
-
- (Hash) to_hash
Returns the hash representation of the arguments to this operation.
-
- (String) to_json
Returns the JSON representation of the arguments to this operation.
Constructor Details
- (Operation) initialize(args = [])
Initializes a new operation with the given arguments.
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
- (Object) context (readonly)
96 97 98 |
# File 'lib/rack/rpc/operation.rb', line 96 def context @context end |
Class Method Details
+ (Range) arity
Returns the arity range for this operation class.
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, | min += 1 unless [:optional].eql?(true) max += 1 end Range.new(min, max) end end end |
+ execute { ... }
This method returns an undefined value.
Defines the #execute
instance method.
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 = {})
This method returns an undefined value.
Defines an operand for this operation class.
21 22 23 24 |
# File 'lib/rack/rpc/operation.rb', line 21 def self.operand(name, type = Object, = {}) raise TypeError, "expected a Class, but got #{type.inspect}" unless type.is_a?(Class) operands[name.to_sym] = .merge(:type => type) end |
+ (Hash{Symbol => Hash}) operands
Returns the operand definitions for this operation class.
71 72 73 |
# File 'lib/rack/rpc/operation.rb', line 71 def self.operands @operands ||= {} end |
+ prepare { ... }
This method returns an undefined value.
Defines the #prepare
instance method.
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
- execute
This method returns an undefined value.
Executes this operation.
192 193 194 |
# File 'lib/rack/rpc/operation.rb', line 192 def execute raise NotImplementedError, "#{self.class}#execute" end |
- prepare
This method returns an undefined value.
Prepares this operation.
183 184 185 |
# File 'lib/rack/rpc/operation.rb', line 183 def prepare self end |
- (Array) to_a
Returns the array representation of the arguments to this operation.
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, )| result << instance_variable_get("@#{param_name}") result end end |
- (Hash) to_hash
Returns the hash representation of the arguments to this operation.
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, )| result[param_name] = instance_variable_get("@#{param_name}") result end end |
- (String) to_json
Returns the JSON representation of the arguments to this operation.
222 223 224 |
# File 'lib/rack/rpc/operation.rb', line 222 def to_json to_hash.to_json end |