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
# File 'lib/speedflow/flow.rb', line 14

def initialize(config)
  @config = config
end

Instance Method Details

#add_config_argument(arguments) ⇒ Object

Public: Add config arguement

arguments - Hash of arguments.

Returns Hash of arguments.



45
46
47
48
49
50
51
52
# File 'lib/speedflow/flow.rb', line 45

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.

Returns flat Array of arguments.



77
78
79
80
81
82
83
84
85
# File 'lib/speedflow/flow.rb', line 77

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.



90
91
92
# File 'lib/speedflow/flow.rb', line 90

def plugin_manager
  @plugin_manager ||= PluginManager.new(@config['plugins'])
end

#transform_arguments(arguments, prev_values, inputs) ⇒ Object

Public: Transform arguments (to add value)

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

Returns Hash of arguments.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/speedflow/flow.rb', line 61

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.nil? && arg_values.key?('default')
      arguments[arg_name]['value'] = arg_values['default']
    end
  end
end

#trigger(trigger, inputs) ⇒ Object

Public: Trigger.

trigger - Trigger name. inputs - Inputs.

Returns nothing.



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

def trigger(trigger, inputs)
  unless @config.flow_trigger?(trigger)
    raise FlowTriggerNotFound, "Unable to trigger: #{trigger}"
  end

  output = {}
  @config['flow'][trigger.to_s].each do |step|
    arguments = step['arguments'] || {}

    step['arguments'] = transform_arguments(arguments, output, inputs)
    step['arguments'] = add_config_argument(arguments)

    output = plugin_manager.call_action_from_step(step)
  end
end