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
- #context ⇒ Object readonly
Class Method Summary collapse
-
.arity ⇒ Range
Returns the arity range for this operation class.
-
.execute { ... } ⇒ void
Defines the ‘#execute` instance method.
-
.operand(name, type = Object, options = {}) ⇒ void
Defines an operand for this operation class.
-
.operands ⇒ Hash{Symbol => Hash}
Returns the operand definitions for this operation class.
-
.prepare { ... } ⇒ void
Defines the ‘#prepare` instance method.
Instance Method Summary collapse
-
#execute ⇒ void
abstract
Executes this operation.
-
#initialize(args = []) ⇒ Operation
constructor
Initializes a new operation with the given arguments.
-
#prepare ⇒ void
abstract
Prepares this operation.
-
#to_a ⇒ Array
Returns the array representation of the arguments to this operation.
-
#to_hash ⇒ Hash
Returns the hash representation of the arguments to this operation.
-
#to_json ⇒ String
Returns the JSON representation of the arguments to this operation.
Constructor Details
#initialize(args = []) ⇒ Operation
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
#context ⇒ Object (readonly)
96 97 98 |
# File 'lib/rack/rpc/operation.rb', line 96 def context @context end |
Class Method Details
.arity ⇒ Range
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 { ... } ⇒ void
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 = {}) ⇒ void
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 |
.operands ⇒ Hash{Symbol => Hash}
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 { ... } ⇒ void
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 ⇒ void
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 ⇒ void
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_a ⇒ Array
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 |
#to_hash ⇒ 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 |
#to_json ⇒ String
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 |