class Speedflow::Flow

Used to manage the flow of Speedflow

Public Class Methods

new(config) click to toggle source

Public: Constructor.

config - Default Hahs of config.

Examples

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

Returns a Hash of config.

# File lib/speedflow/flow.rb, line 14
def initialize(config)
  # TODO: Reinforce the SOI.
  @config = config
end

Public Instance Methods

add_config_argument(arguments) click to toggle source

Public: Add config arguement

TODO: Move to a normal object: TriggerArgument

arguments - Hash of arguments.

Returns Hash of arguments.

# 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_arguments() click to toggle source

Public: Get flat arguments from flow.

TODO: Move to a normal object: TriggerArgument

Returns flat Array of arguments.

# 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_manager() click to toggle source

Plugin manager.

Returns an instance of plugin manager.

# File lib/speedflow/flow.rb, line 118
def plugin_manager
  @plugin_manager ||= Plugin::Manager.new(@config['plugins'])
end
transform_arguments(arguments, prev_values, inputs) click to toggle source

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.

# 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) click to toggle source

Public: Trigger.

trigger - Trigger name. input - Input Hash.

Returns nothing.

# 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) click to toggle source

Public: Trigger step

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

Returns output Hash.

# 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