Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started to refactor instance methods for AASM objects
- Loading branch information
Showing
5 changed files
with
87 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module AASM | ||
class InstanceBase | ||
|
||
def initialize(instance) | ||
@instance = instance | ||
end | ||
|
||
def current_state | ||
@current_state ||= persistable? ? @instance.aasm_read_state : @instance.aasm_enter_initial_state | ||
end | ||
|
||
def current_state=(state) | ||
if @instance.respond_to?(:aasm_write_state_without_persistence) || @instance.private_methods.include?('aasm_write_state_without_persistence') | ||
@instance.aasm_write_state_without_persistence(state) | ||
end | ||
@current_state = state | ||
end | ||
|
||
def human_state | ||
AASM::Localizer.new.human_state_name(@instance.class, current_state) | ||
end | ||
|
||
def events(state=current_state) | ||
events = @instance.class.aasm_events.values.select {|e| e.transitions_from_state?(state) } | ||
events.map {|e| e.name} | ||
end | ||
|
||
def state_object_for_name(name) | ||
obj = @instance.class.aasm.states.find {|s| s == name} | ||
raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil? | ||
obj | ||
end | ||
|
||
def determine_state_name(state) | ||
case state | ||
when Symbol, String | ||
state | ||
when Proc | ||
state.call(@instance) | ||
else | ||
raise NotImplementedError, "Unrecognized state-type given. Expected Symbol, String, or Proc." | ||
end | ||
end | ||
|
||
def may_fire_event?(name, *args) | ||
event = @instance.class.aasm.events[name] | ||
event.may_fire?(@instance, *args) | ||
end | ||
|
||
private | ||
|
||
def persistable? | ||
@instance.respond_to?(:aasm_read_state) || @instance.private_methods.include?('aasm_read_state') | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters