Module: StateMachine::Integrations

Defined in:
lib/state_machine/integrations.rb,
lib/state_machine/integrations/sequel.rb,
lib/state_machine/integrations/data_mapper.rb,
lib/state_machine/integrations/active_record.rb,
lib/state_machine/integrations/data_mapper/observer.rb,
lib/state_machine/integrations/active_record/observer.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ActiveRecord, DataMapper, Sequel

Class Method Summary collapse

Class Method Details

.find(name) ⇒ Object

Finds an integration with the given name. If the integration cannot be found, then a NameError exception will be raised.

Examples

StateMachine::Integrations.find(:active_record)   # => StateMachine::Integrations::ActiveRecord
StateMachine::Integrations.find(:data_mapper)     # => StateMachine::Integrations::DataMapper
StateMachine::Integrations.find(:sequel)          # => StateMachine::Integrations::Sequel
StateMachine::Integrations.find(:invalid)         # => NameError: wrong constant name Invalid


64
65
66
# File 'lib/state_machine/integrations.rb', line 64

def self.find(name)
  const_get(name.to_s.gsub(/(?:^|_)(.)/) {$1.upcase})
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 StateMachine::Integrations namespace and find one that successfully matches the class.

Examples

class Vehicle
end

class ARVehicle < ActiveRecord::Base
end

class DMVehicle
  include DataMapper::Resource
end

class SequelVehicle < Sequel::Model
end

StateMachine::Integrations.match(Vehicle)         # => nil
StateMachine::Integrations.match(ARVehicle)       # => StateMachine::Integrations::ActiveRecord
StateMachine::Integrations.match(DMVehicle)       # => StateMachine::Integrations::DataMapper
StateMachine::Integrations.match(SequelVehicle)   # => StateMachine::Integrations::Sequel


49
50
51
52
53
# File 'lib/state_machine/integrations.rb', line 49

def self.match(klass)
  if integration = constants.find {|name| const_get(name).matches?(klass)}
    find(integration)
  end
end