class Action::Plan

Constants

RunnableStatuses
VERSION

Attributes

schedule[R]

Public Class Methods

json_create(hash_plan) click to toggle source
# File lib/action/plan.rb, line 51
def self.json_create hash_plan
  schedule = hash_plan["schedule"].map{|h| Action::State.new(h)}
  Action::Plan.new.tap{ |plan|
    plan.instance_variable_set(:@schedule, schedule)
  }
end
new() { |dsl| ... } click to toggle source
# File lib/action/plan.rb, line 10
def initialize
  @schedule = []
  yield DSL.new(self) if block_given?
end

Public Instance Methods

==(other) click to toggle source
# File lib/action/plan.rb, line 58
def == other
  self.class == other.class && schedule == other.action_states
end
action_states() click to toggle source
# File lib/action/plan.rb, line 31
def action_states
  schedule.dup
end
plan_action(action_class, &block) click to toggle source

let action plan itself

# File lib/action/plan.rb, line 77
def plan_action action_class, &block
  action_class.new.configure(&block).expand_into(plan: self)
end
run(&block) click to toggle source
# File lib/action/plan.rb, line 15
def run &block
  raise NotRunnable, "#{status} plan can't be run" unless runnable?
  schedule.each do |state|
    next if state.status == :done
    action = state.create_action
    state.on(:status_changed) do |state, new_status, old_status|
      broadcast :plan_state_changed, self, state, new_status, old_status
    end
    action.on(:progress) do |done, total|
      broadcast :action_progress, self, action, done, total
    end
    run_action action, state, &block
    break unless state.status == :done
  end
end
runnable?() click to toggle source
# File lib/action/plan.rb, line 40
def runnable?
  RunnableStatuses.include?(status)
end
schedule_action(action_class, config) click to toggle source

schedule action execution at the current point in the plan

# File lib/action/plan.rb, line 82
def schedule_action action_class, config
  new_state = Action::State.new(action_class: action_class, config: config)
  schedule << new_state
  new_state.status = :planned
end
status() click to toggle source
# File lib/action/plan.rb, line 35
def status
  Action::State.sequence_status(schedule.map(&:status))
end
to_json(*options) click to toggle source
# File lib/action/plan.rb, line 44
def to_json *options
  {
    JSON.create_id => self.class.name,
    schedule: schedule
  }.to_json(*options)
end

Private Instance Methods

run_action(action, state) click to toggle source
# File lib/action/plan.rb, line 92
def run_action action, state
  state.status = :running
  action.run
  state.status = :done
rescue Exception => e
  state.status = :failed
  broadcast :action_failure, self, action, e
end