module EnumStateMachine::Integrations
Integrations
allow state machines to take advantage of features within the context of a particular library. This is currently most useful with database libraries. For example, the various database integrations allow state machines to hook into features like:
-
Saving
-
Transactions
-
Observers
-
Scopes
-
Callbacks
-
Validation errors
This type of integration allows the user to work with state machines in a fashion similar to other object models in their application.
The integration interface is loosely defined by various unimplemented methods in the EnumStateMachine::Machine
class. See that class or the various built-in integrations for more information about how to define additional integrations.
Public Class Methods
Gets a list of all of the available integrations for use. This will always list the ActiveModel
integration last.
Example¶ ↑
EnumStateMachine::Integrations.all # => [EnumStateMachine::Integrations::ActiveRecord, EnumStateMachine::Integrations::ActiveModel]
# File lib/enum_state_machine/integrations.rb 92 def self.all 93 constants = self.constants.map {|c| c.to_s}.select {|c| c != 'ActiveModel'}.sort << 'ActiveModel' 94 constants.map {|c| const_get(c)} 95 end
Finds an integration with the given name. If the integration cannot be found, then a NameError exception will be raised.
Examples¶ ↑
EnumStateMachine::Integrations.find_by_name(:active_record) # => EnumStateMachine::Integrations::ActiveRecord EnumStateMachine::Integrations.find_by_name(:active_model) # => EnumStateMachine::Integrations::ActiveModel EnumStateMachine::Integrations.find_by_name(:invalid) # => EnumStateMachine::IntegrationNotFound: :invalid is an invalid integration
# File lib/enum_state_machine/integrations.rb 81 def self.find_by_name(name) 82 all.detect {|integration| integration.integration_name == name} || raise(IntegrationNotFound.new(name)) 83 end
Attempts to find an integration that matches the given class. This will look through all of the built-in integrations under the EnumStateMachine::Integrations
namespace and find one that successfully matches the class.
Examples¶ ↑
class Vehicle end class ActiveModelVehicle include ActiveModel::Observing include ActiveModel::Validations end class ActiveRecordVehicle < ActiveRecord::Base end EnumStateMachine::Integrations.match(Vehicle) # => nil EnumStateMachine::Integrations.match(ActiveModelVehicle) # => EnumStateMachine::Integrations::ActiveModel EnumStateMachine::Integrations.match(ActiveRecordVehicle) # => EnumStateMachine::Integrations::ActiveRecord
# File lib/enum_state_machine/integrations.rb 57 def self.match(klass) 58 all.detect {|integration| integration.matches?(klass)} 59 end
Attempts to find an integration that matches the given list of ancestors. This will look through all of the built-in integrations under the EnumStateMachine::Integrations
namespace and find one that successfully matches one of the ancestors.
Examples¶ ↑
EnumStateMachine::Integrations.match([]) # => nil EnumStateMachine::Integrations.match(['ActiveRecord::Base') # => EnumStateMachine::Integrations::ActiveModel
# File lib/enum_state_machine/integrations.rb 69 def self.match_ancestors(ancestors) 70 all.detect {|integration| integration.matches_ancestors?(ancestors)} 71 end