Sha256: 454086a95f31ea85d119acbe2ef5e98015b45548fd2e23e5af2fd58fb4032f1a

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module StateMachines
  module Integrations
    # Provides a set of base helpers for managing individual integrations
    module Base
      module ClassMethods
        # The default options to use for state machines using this integration
        attr_reader :defaults

        # The name of the integration
        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

        # Whether this integration is available for the current library.  This
        # is only true if the ORM that the integration is for is currently
        # defined.
        def available?
          matching_ancestors.any? && Object.const_defined?(matching_ancestors[0].split('::')[0])
        end

        # The list of ancestor names that cause this integration to matched.
        def matching_ancestors
          []
        end

        # Whether the integration should be used for the given class.
        def matches?(klass)
          matches_ancestors?(klass.ancestors.map { |ancestor| ancestor.name })
        end

        # Whether the integration should be used for the given list of ancestors.
        def matches_ancestors?(ancestors)
          (ancestors & matching_ancestors).any?
        end

      end

      extend ClassMethods

      def self.included(base) #:nodoc:
        base.class_eval { extend ClassMethods }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
state_machines-0.1.1 lib/state_machines/integrations/base.rb
state_machines-0.1.0 lib/state_machines/integrations/base.rb
state_machines-0.0.2 lib/state_machines/integrations/base.rb