Skip to content

Commit

Permalink
prevent reusing reserved state names, or reusing state names in general
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Pepin-Perreault committed Apr 25, 2017
1 parent 49e36a2 commit 44a4cc9
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rworkflow/lifecycle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Lifecycle

CARDINALITY_ALL_STARTED = :all_started # Indicates a cardinality equal to the jobs pushed at the start of the workflow

RESERVED_STATE_NAMES = [Rworkflow::Flow::STATE_FAILED, Rworkflow::Flow::STATE_SUCCESSFUL].map(&:to_s).freeze
DEFAULT_CARDINALITY = State::DEFAULT_CARDINALITY
STATE_POLICY_NO_WAIT = State::STATE_POLICY_NO_WAIT
DEFAULT_STATE_OPTIONS = {
Expand All @@ -15,7 +16,7 @@ class Lifecycle
def initialize(state_class: State, state_options: {})
@state_options = DEFAULT_STATE_OPTIONS.merge(state_options)
@state_class = state_class
@states = {}
@states = {}.with_indifferent_access
@default = nil
yield(self) if block_given?
end
Expand All @@ -24,6 +25,9 @@ def state(name, options = {})
options = @state_options.merge(options)
new_state = @state_class.new(**options)

raise ArgumentError, 'given state name is a reserved state name' if RESERVED_STATE_NAMES.include?(name.to_s)
raise ArgumentError, 'no two states can have the same name in a lifecycle' if @states.key?(name)

yield(new_state) if block_given?

@states[name] = new_state
Expand Down

0 comments on commit 44a4cc9

Please sign in to comment.