Module: StateMachine::Integrations::Base::ClassMethods
- Included in:
- StateMachine::Integrations::Base
- Defined in:
- lib/state_machine/integrations/base.rb
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
The default options to use for state machines using this integration.
Instance Method Summary collapse
-
#available? ⇒ Boolean
Whether this integration is available for the current library.
-
#extended(base) ⇒ Object
Extends the given object with any version overrides that are currently active.
-
#integration_name ⇒ Object
The name of the integration.
-
#locale_path ⇒ Object
The path to the locale file containing translations for this integration.
-
#matches?(klass) ⇒ Boolean
Whether the integration should be used for the given class.
-
#matches_ancestors?(ancestors) ⇒ Boolean
Whether the integration should be used for the given list of ancestors.
-
#matching_ancestors ⇒ Object
The list of ancestor names that cause this integration to matched.
-
#version(name, &block) ⇒ Object
Creates a new version override for an integration.
-
#versions ⇒ Object
Tracks the various version overrides for an integration.
Instance Attribute Details
#defaults ⇒ Object (readonly)
The default options to use for state machines using this integration
7 8 9 |
# File 'lib/state_machine/integrations/base.rb', line 7 def defaults @defaults end |
Instance Method Details
#available? ⇒ Boolean
Whether this integration is available for the current library. This is only true if the ORM that the integration is for is currently defined.
23 24 25 |
# File 'lib/state_machine/integrations/base.rb', line 23 def available? matching_ancestors.any? && Object.const_defined?(matching_ancestors[0].split('::')[0]) end |
#extended(base) ⇒ Object
Extends the given object with any version overrides that are currently active
86 87 88 89 90 |
# File 'lib/state_machine/integrations/base.rb', line 86 def extended(base) versions.each do |version| base.extend(version) if version.active? end end |
#integration_name ⇒ Object
The name of the integration
10 11 12 13 14 15 16 17 18 |
# File 'lib/state_machine/integrations/base.rb', line 10 def integration_name @integration_name ||= begin name = self.name.split('::').last name.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') name.gsub!(/([a-z\d])([A-Z])/,'\1_\2') name.downcase! name.to_sym end end |
#locale_path ⇒ Object
The path to the locale file containing translations for this integration. This file will only exist for integrations that actually support i18n.
79 80 81 82 |
# File 'lib/state_machine/integrations/base.rb', line 79 def locale_path path = "#{File.dirname(__FILE__)}/#{integration_name}/locale.rb" path if File.exists?(path) end |
#matches?(klass) ⇒ Boolean
Whether the integration should be used for the given class.
33 34 35 |
# File 'lib/state_machine/integrations/base.rb', line 33 def matches?(klass) matches_ancestors?(klass.ancestors.map {|ancestor| ancestor.name}) end |
#matches_ancestors?(ancestors) ⇒ Boolean
Whether the integration should be used for the given list of ancestors.
38 39 40 |
# File 'lib/state_machine/integrations/base.rb', line 38 def matches_ancestors?(ancestors) (ancestors & matching_ancestors).any? end |
#matching_ancestors ⇒ Object
The list of ancestor names that cause this integration to matched.
28 29 30 |
# File 'lib/state_machine/integrations/base.rb', line 28 def matching_ancestors [] end |
#version(name, &block) ⇒ Object
Creates a new version override for an integration. When this integration is activated, each version that is marked as active will also extend the integration.
Example
module StateMachine
module Integrations
module ORMLibrary
version '0.2.x - 0.3.x' do
def self.active?
::ORMLibrary::VERSION >= '0.2.0' && ::ORMLibrary::VERSION < '0.4.0'
end
def invalidate(object, attribute, , values = [])
# Override here...
end
end
end
end
end
In the above example, a version override is defined for the ORMLibrary integration when the version is between 0.2.x and 0.3.x.
71 72 73 74 |
# File 'lib/state_machine/integrations/base.rb', line 71 def version(name, &block) versions << mod = Module.new(&block) mod end |
#versions ⇒ Object
Tracks the various version overrides for an integration
43 44 45 |
# File 'lib/state_machine/integrations/base.rb', line 43 def versions @versions ||= [] end |