Class: Hyperloop::Operation
- Defined in:
- lib/hyper-operation/api.rb,
lib/hyper-operation/railway.rb,
lib/hyper-operation/version.rb,
lib/hyper-operation/exception.rb,
lib/hyper-operation/railway/run.rb,
lib/hyper-operation/call_by_class_name.rb,
lib/hyper-operation/railway/dispatcher.rb,
lib/hyper-operation/railway/validations.rb,
lib/hyper-operation/railway/params_wrapper.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Exit, ParamsWrapper, Railway, ValidationException
Constant Summary collapse
- VERSION =
'0.5.7'
Class Method Summary collapse
- ._Railway ⇒ Object
- ._run(*args) ⇒ Object
- .add_error(param, symbol, message, *args, &block) ⇒ Object
- .async(*args, &block) ⇒ Object
- .fail(*args, &block) ⇒ Object
- .failed(*args, &block) ⇒ Object
- .inherited(child) ⇒ Object
- .on_dispatch(&block) ⇒ Object
- .outbound(*keys) ⇒ Object
- .param(*args, &block) ⇒ Object
- .run(*args) ⇒ Object
- .step(*args, &block) ⇒ Object
- .then(*args, &block) ⇒ Object
- .validate(*args, &block) ⇒ Object
Instance Method Summary collapse
- #abort!(arg = nil) ⇒ Object
- #add_error(key, kind, message = nil) ⇒ Object
- #has_errors? ⇒ Boolean
-
#initialize ⇒ Operation
constructor
A new instance of Operation.
- #params ⇒ Object
- #succeed!(arg = nil) ⇒ Object
Constructor Details
#initialize ⇒ Operation
Returns a new instance of Operation.
34 35 36 |
# File 'lib/hyper-operation/api.rb', line 34 def initialize @_railway = self.class._Railway.new(self) end |
Class Method Details
._Railway ⇒ Object
95 96 97 |
# File 'lib/hyper-operation/api.rb', line 95 def _Railway self.singleton_class._Railway end |
._run(*args) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/hyper-operation/api.rb', line 44 def _run(*args) new.instance_eval do @_railway.process_params(args) @_railway.process_validations @_railway.run @_railway.dispatch @_railway.result end end |
.add_error(param, symbol, message, *args, &block) ⇒ Object
75 76 77 |
# File 'lib/hyper-operation/api.rb', line 75 def add_error(param, symbol, , *args, &block) _Railway.add_error(param, symbol, , *args, &block) end |
.async(*args, &block) ⇒ Object
87 88 89 |
# File 'lib/hyper-operation/api.rb', line 87 def async(*args, &block) _Railway.add_async(*args, &block) end |
.fail(*args, &block) ⇒ Object
58 59 60 |
# File 'lib/hyper-operation/api.rb', line 58 def fail(*args, &block) run(*args).fail(&block) end |
.failed(*args, &block) ⇒ Object
83 84 85 |
# File 'lib/hyper-operation/api.rb', line 83 def failed(*args, &block) _Railway.add_failed(*args, &block) end |
.inherited(child) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/hyper-operation/api.rb', line 99 def inherited(child) child.singleton_class.define_singleton_method(:param) do |*args, &block| _Railway.add_param(*args, &block) end child.singleton_class.define_singleton_method(:outbound) do |*keys| keys.each { |key| _Railway.add_param(key => nil, :type => :outbound) } end child.singleton_class.define_singleton_method(:validate) do |*args, &block| _Railway.add_validation(*args, &block) end child.singleton_class.define_singleton_method(:add_error) do |param, symbol, , *args, &block| _Railway.add_error(param, symbol, , *args, &block) end child.singleton_class.define_singleton_method(:step) do |*args, &block| _Railway.add_step({scope: :class}, *args, &block) end child.singleton_class.define_singleton_method(:failed) do |*args, &block| _Railway.add_failed({scope: :class}, *args, &block) end child.singleton_class.define_singleton_method(:async) do |*args, &block| _Railway.add_async({scope: :class}, *args, &block) end child.singleton_class.define_singleton_method(:_Railway) do Hyperloop::Context.set_var(self, :@_railway) do # overcomes a bug in Opal 0.9 which returns nil for singleton superclass my_super = superclass || `self.$$singleton_of`.superclass.singleton_class if my_super == Operation.singleton_class Class.new(Railway) else Class.new(my_super._Railway).tap do |wrapper| [:@validations, :@tracks, :@receivers].each do |var| value = my_super._Railway.instance_variable_get(var) wrapper.instance_variable_set(var, value && value.dup) end end end end end end |
.on_dispatch(&block) ⇒ Object
91 92 93 |
# File 'lib/hyper-operation/api.rb', line 91 def on_dispatch(&block) _Railway.add_receiver(&block) end |
.outbound(*keys) ⇒ Object
66 67 68 69 |
# File 'lib/hyper-operation/api.rb', line 66 def outbound(*keys) keys.each { |key| _Railway.add_param(key => nil, :type => :outbound) } #singleton_class.outbound(*keys) end |
.param(*args, &block) ⇒ Object
62 63 64 |
# File 'lib/hyper-operation/api.rb', line 62 def param(*args, &block) _Railway.add_param(*args, &block) end |
.run(*args) ⇒ Object
40 41 42 |
# File 'lib/hyper-operation/api.rb', line 40 def run(*args) _run(*args) end |
.step(*args, &block) ⇒ Object
79 80 81 |
# File 'lib/hyper-operation/api.rb', line 79 def step(*args, &block) _Railway.add_step(*args, &block) end |
.then(*args, &block) ⇒ Object
54 55 56 |
# File 'lib/hyper-operation/api.rb', line 54 def then(*args, &block) run(*args).then(&block) end |
.validate(*args, &block) ⇒ Object
71 72 73 |
# File 'lib/hyper-operation/api.rb', line 71 def validate(*args, &block) _Railway.add_validation(*args, &block) end |
Instance Method Details
#abort!(arg = nil) ⇒ Object
26 27 28 |
# File 'lib/hyper-operation/api.rb', line 26 def abort!(arg = nil) Railway.abort!(arg) end |
#add_error(key, kind, message = nil) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/hyper-operation/api.rb', line 4 def add_error(key, kind, = nil) raise ArgumentError.new("Invalid kind") unless kind.is_a?(Symbol) @errors ||= Mutations::ErrorHash.new @errors.tap do |errs| path = key.to_s.split(".") last = path.pop inner = path.inject(errs) do |cur_errors,part| cur_errors[part.to_sym] ||= Mutations::ErrorHash.new end inner[last] = Mutations::ErrorAtom.new(key, kind, :message => ) end end |
#has_errors? ⇒ Boolean
18 19 20 |
# File 'lib/hyper-operation/api.rb', line 18 def has_errors? !@errors.nil? end |
#params ⇒ Object
22 23 24 |
# File 'lib/hyper-operation/api.rb', line 22 def params @params end |