From 44a4cc92d4863c90d0e96f73828ea8af140727fc Mon Sep 17 00:00:00 2001 From: Nicolas Pepin-Perreault Date: Tue, 25 Apr 2017 17:16:32 +0200 Subject: [PATCH] prevent reusing reserved state names, or reusing state names in general --- lib/rworkflow/lifecycle.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/rworkflow/lifecycle.rb b/lib/rworkflow/lifecycle.rb index 162e730..927505f 100644 --- a/lib/rworkflow/lifecycle.rb +++ b/lib/rworkflow/lifecycle.rb @@ -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 = { @@ -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 @@ -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