Class: Houston::Actions

Inherits:
Object
  • Object
show all
Defined in:
lib/houston/boot/actions.rb

Defined Under Namespace

Classes: Action, ExecutionContext

Instance Method Summary collapse

Constructor Details

#initializeActions



7
8
9
# File 'lib/houston/boot/actions.rb', line 7

def initialize
  @actions = Concurrent::Hash.new
end

Instance Method Details

#[](name) ⇒ Object



24
25
26
# File 'lib/houston/boot/actions.rb', line 24

def [](name)
  actions[name]
end

#countObject



16
17
18
# File 'lib/houston/boot/actions.rb', line 16

def count
  actions.count
end

#define(name, required_params = [], &block) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
# File 'lib/houston/boot/actions.rb', line 32

def define(name, required_params=[], &block)
  raise ArgumentError, "#{name.inspect} is already defined" if exists?(name)
  redefine(name, required_params, &block)
end

#exists?(name) ⇒ Boolean



20
21
22
# File 'lib/houston/boot/actions.rb', line 20

def exists?(name)
  actions.key?(name)
end

#namesObject



12
13
14
# File 'lib/houston/boot/actions.rb', line 12

def names
  actions.keys
end

#redefine(name, required_params = [], &block) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
# File 'lib/houston/boot/actions.rb', line 37

def redefine(name, required_params=[], &block)
  raise ArgumentError, "A block is required to define an action" unless block_given?
  actions[name] = Action.new(name, required_params, block)
end

#run(name, params = {}, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/houston/boot/actions.rb', line 48

def run(name, params={}, options={})
  raise ArgumentError, "#{name.inspect} is not defined" unless exists?(name)
  action = actions.fetch(name)

  unless params.is_a?(Hash)
    raise ArgumentError, "params must be a Hash" unless params.respond_to?(:to_h)
    params = params.to_h
  end

  assert_required_params! action, params
  assert_serializable! params

  Houston.async(options.fetch(:async, true)) do
    action.send :run!, params, options
  end
end

#to_aObject



28
29
30
# File 'lib/houston/boot/actions.rb', line 28

def to_a
  actions.values
end

#undefine(name) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
# File 'lib/houston/boot/actions.rb', line 42

def undefine(name)
  raise ArgumentError, "#{name.inspect} is not defined" unless exists?(name)
  actions.delete name
end