Class: Hyperloop::Operation

Inherits:
Object
  • Object
show all
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/railway/dispatcher.rb,
lib/hyper-operation/railway/validations.rb,
lib/hyper-operation/railway/params_wrapper.rb

Direct Known Subclasses

Application::Boot, ServerOp

Defined Under Namespace

Classes: Exit, ParamsWrapper, Railway, ValidationException

Constant Summary collapse

VERSION =
'0.99.6'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOperation

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

._RailwayObject



100
101
102
# File 'lib/hyper-operation/api.rb', line 100

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



80
81
82
# File 'lib/hyper-operation/api.rb', line 80

def add_error(param, symbol, message, *args, &block)
  _Railway.add_error(param, symbol, message, *args, &block)
end

.async(*args, &block) ⇒ Object



92
93
94
# File 'lib/hyper-operation/api.rb', line 92

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



88
89
90
# File 'lib/hyper-operation/api.rb', line 88

def failed(*args, &block)
  _Railway.add_failed(*args, &block)
end

.inbound(*args, &block) ⇒ Object



66
67
68
69
# File 'lib/hyper-operation/api.rb', line 66

def inbound(*args, &block)
  name, opts = ParamsWrapper.get_name_and_opts(*args)
  _Railway.add_param(name, opts.merge(inbound: :true), &block)
end

.inherited(child) ⇒ Object



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
146
147
148
149
150
151
152
153
154
155
# File 'lib/hyper-operation/api.rb', line 104

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(:inbound) do |*args, &block|
    name, opts = ParamsWrapper.get_name_and_opts(*args)
    _Railway.add_param(name, opts.merge(inbound: :true), &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, message, *args, &block|
    _Railway.add_error(param, symbol, message, *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



96
97
98
# File 'lib/hyper-operation/api.rb', line 96

def on_dispatch(&block)
  _Railway.add_receiver(&block)
end

.outbound(*keys) ⇒ Object



71
72
73
74
# File 'lib/hyper-operation/api.rb', line 71

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



84
85
86
# File 'lib/hyper-operation/api.rb', line 84

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



76
77
78
# File 'lib/hyper-operation/api.rb', line 76

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

Raises:

  • (ArgumentError)


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, message = 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 => message)
  end
end

#has_errors?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/hyper-operation/api.rb', line 18

def has_errors?
  !@errors.nil?
end

#paramsObject



22
23
24
# File 'lib/hyper-operation/api.rb', line 22

def params
  @params
end

#succeed!(arg = nil) ⇒ Object



30
31
32
# File 'lib/hyper-operation/api.rb', line 30

def succeed!(arg = nil)
  Railway.succeed!(arg)
end