Class: Speedflow::Flow

Inherits:
Object
  • Object
show all
Defined in:
lib/speedflow/flow.rb

Overview

Used to manage the flow of Speedflow

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Flow

Public: Constructor.

config - Default Hahs of config.

Examples

Flow.new({})
# => <Speedflow::Flow>

Returns a Hash of config.



14
15
16
17
# File 'lib/speedflow/flow.rb', line 14

def initialize(config)
  # TODO: Reinforce the SOI.
  @config = config
end

Instance Method Details

#add_config_argument(arguments) ⇒ Object

Public: Add config arguement

TODO: Move to a normal object: TriggerArgument

arguments - Hash of arguments.

Returns Hash of arguments.



69
70
71
72
73
74
75
76
# File 'lib/speedflow/flow.rb', line 69

def add_config_argument(arguments)
  arguments['_config'] = @config.clone
  arguments['_config'].delete_if { |k, _| arguments.key?(k) }
  arguments['_config'].delete_if do |k, _|
    @config.class.const_get('DEFAULTS').stringify_keys.key?(k)
  end
  arguments
end

#flat_argumentsObject

Public: Get flat arguments from flow.

TODO: Move to a normal object: TriggerArgument

Returns flat Array of arguments.



105
106
107
108
109
110
111
112
113
# File 'lib/speedflow/flow.rb', line 105

def flat_arguments
  args = []
  @config['flow'].each do |_, steps|
    steps.each do |s|
      (args << s['arguments'].keys).flatten! unless s['arguments'].nil?
    end
  end
  args.uniq
end

#plugin_managerObject

Plugin manager.

Returns an instance of plugin manager.



118
119
120
# File 'lib/speedflow/flow.rb', line 118

def plugin_manager
  @plugin_manager ||= Plugin::Manager.new(@config['plugins'])
end

#transform_arguments(arguments, prev_values, inputs) ⇒ Object

Public: Transform arguments (to add value)

TODO: Move to a normal object: TriggerArgument

arguments - Hash of arguments. prev_values - Hash of previous values. inputs - Hash of inputs.

Returns Hash of arguments.



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/speedflow/flow.rb', line 87

def transform_arguments(arguments, prev_values, inputs)
  arguments = arguments.replace_values_from_previous(prev_values)
  arguments.each do |arg_name, arg_values|
    arguments[arg_name] = {} unless arguments[arg_name].is_a?(Hash)
    arguments[arg_name]['value'] = ''
    if inputs.key?(arg_name)
      arguments[arg_name]['value'] = inputs[arg_name]
    elsif arg_values.is_a?(Hash) && arg_values.key?('default')
      arguments[arg_name]['value'] = arg_values['default']
    end
  end
end

#trigger(trigger, input) ⇒ Object

Public: Trigger.

trigger - Trigger name. input - Input Hash.

Returns nothing.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/speedflow/flow.rb', line 25

def trigger(trigger, input)
  # TODO: Change by: Configuration.not_empty_key?
  # TODO: Move to this class: Flow.trigger?
  unless @config.flow_trigger?(trigger)
    raise FlowTriggerNotFound, "Unable to trigger: #{trigger}"
  end

  # TODO: Move in a normal object tree: Flow > Trigger > Action
  output_by_tag = {}
  output = {}
  @config['flow'][trigger.to_s].each do |step|
    output = trigger_step(step, input, output, output_by_tag)
  end
end

#trigger_step(step, input, output, output_by_tag) ⇒ Object

Public: Trigger step

step - Step Hash. input - Input Hash. output - Output Hash. output_by_tag - Output by tag Hash.

Returns output Hash.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/speedflow/flow.rb', line 48

def trigger_step(step, input, output, output_by_tag)
  arguments = step['arguments'] || {}

  # TODO: Move to a normal object: TriggerArgument
  step['arguments'] = transform_arguments(arguments, output, input)
  step['arguments'] = add_config_argument(arguments)

  # TODO: Move to a normal object: TriggerArgument
  output = plugin_manager.action_from_step(step)
  output_by_tag[step['tag']] = output if step.key? 'tag'

  output.merge(output_by_tag)
end