Module: EnumStateMachine::Integrations

Defined in:
lib/enum_state_machine/integrations.rb,
lib/enum_state_machine/integrations/base.rb,
lib/enum_state_machine/integrations/active_model.rb,
lib/enum_state_machine/integrations/active_record.rb,
lib/enum_state_machine/integrations/active_model/observer.rb,
lib/enum_state_machine/integrations/active_model/versions.rb,
lib/enum_state_machine/integrations/active_record/versions.rb,
lib/enum_state_machine/integrations/active_model/observer_update.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ActiveModel, ActiveRecord, Base

Class Method Summary collapse

Class Method Details

.allObject

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]


92
93
94
95
# File 'lib/enum_state_machine/integrations.rb', line 92

def self.all
  constants = self.constants.map {|c| c.to_s}.select {|c| c != 'ActiveModel'}.sort << 'ActiveModel'
  constants.map {|c| const_get(c)}
end

.find_by_name(name) ⇒ Object

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


81
82
83
# File 'lib/enum_state_machine/integrations.rb', line 81

def self.find_by_name(name)
  all.detect {|integration| integration.integration_name == name} || raise(IntegrationNotFound.new(name))
end

.match(klass) ⇒ Object

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


57
58
59
# File 'lib/enum_state_machine/integrations.rb', line 57

def self.match(klass)
  all.detect {|integration| integration.matches?(klass)}
end

.match_ancestors(ancestors) ⇒ Object

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


69
70
71
# File 'lib/enum_state_machine/integrations.rb', line 69

def self.match_ancestors(ancestors)
  all.detect {|integration| integration.matches_ancestors?(ancestors)}
end