module StateGate::Builder::TransitionMethods
Description¶ ↑
Multiple private methods allowing StateGate::Builder
to generate transition methods.
-
query the class for all allowed transitions:
Klass.status_transitions # => { pending: [:active], # active: [:suspended, :archived], # suspended: [:active, :archived], # archived: [] }
-
query the class for the allowed transitions for the given state:
Klass.status_transitions_for(:pending) # => [:active] Klass.status_transitions_for(:active) # => [:suspended, :archived]
-
list the allowed transitions from the current state:
.status_transitions # => [:suspended, :archived]
-
query if a given transition is allowed from the current state:
.status_transitions_to?(:active) # => true
Private Instance Methods
Adds an instance method to return an Array of the allowed transitions from the current attribute state.
eg: .status_transitions # => [:active] .status_transitions # => [:suspended, :archived] .status_transitions # => []
# File lib/state_gate/builder/transition_methods.rb, line 109 def _add__instance__attr_transitions(method_name = @attribute) method_name = "#{method_name}_transitions" add__instance__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name} stateables[:#{@attribute}].transitions_for_state(self[:#{@attribute}]) end )) end
Adds an instance method to return TRUE if the current attribute state can transition to the queries status.
eg: .status_transitions_to?(:active) # => true .status_transitions_to?(:archived) # => false
# File lib/state_gate/builder/transition_methods.rb, line 128 def _add__instance__attr_transitions_to(method_name = @attribute) method_name = "#{method_name}_transitions_to?" add__instance__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name}(query_state) test_state = StateGate.symbolize(query_state) stateables[:#{@attribute}].transitions_for_state(self[:#{@attribute}]) .include?(test_state) end )) end
Adds a Class method to return a Hash of the allowed transitions for the attribte
eg: Klass.status_transitions # => { pending: [:active], active: [:suspended, :archived], suspended: [:active, :archived], archived: [] }
# File lib/state_gate/builder/transition_methods.rb, line 66 def _add__klass__attr_transitions(method_name = @attribute) method_name = "#{method_name}_transitions" add__klass__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name} stateables[:#{@attribute}].transitions end )) end
Adds a Class method to return an Array of the allowed attribute transitions for the provided state.
eg: Klass.status_transitions_for(:pending) # => [:active] Klass.status_transitions_for(:active) # => [:suspended, :archived] Klass.status_transitions_for(:dummy) # => ArgumentError
# File lib/state_gate/builder/transition_methods.rb, line 85 def _add__klass__attr_transitions_for(method_name = @attribute) method_name = "#{method_name}_transitions_for" add__klass__helper_method(method_name, __FILE__, __LINE__ - 2, %( def #{method_name}(state) stateables[:#{@attribute}].transitions_for_state(state) end )) end
Add instance methods to the klass that query the allowed transitions
# File lib/state_gate/builder/transition_methods.rb, line 37 def generate_transition_methods _add__klass__attr_transitions _add__klass__attr_transitions_for _add__instance__attr_transitions _add__instance__attr_transitions_to return unless @alias _add__klass__attr_transitions(@alias) _add__klass__attr_transitions_for(@alias) _add__instance__attr_transitions(@alias) _add__instance__attr_transitions_to(@alias) end