Sha256: f3daa9b810b3c7ba16c83c12e35c82d10ca63988a2c381d8838dc00026955f38

Contents?: true

Size: 1.39 KB

Versions: 18

Compression:

Stored size: 1.39 KB

Contents

module StateManager
  module Adapters
    module Base

      module ClassMethods
        # The name of the adapter
        def adapter_name
          @adapter_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 adapter is available for the current library.  This
        # is only true if the ORM that the adapter 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 adapter to matched.
        def matching_ancestors
          []
        end
        
        # Whether the adapter should be used for the given class.
        def matches?(klass)
          matches_ancestors?(klass.ancestors.map {|ancestor| ancestor.name})
        end
        
        # Whether the adapter should be used for the given list of ancestors.
        def matches_ancestors?(ancestors)
          (ancestors & matching_ancestors).any?
        end
      end

      def self.included(base)
        return if base < StateManager::Base
        base.class_eval { extend ClassMethods }
      end

      extend ClassMethods
      
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
state_manager-0.4.0 lib/state_manager/adapters/base.rb
state_manager-0.3.5 lib/state_manager/adapters/base.rb
state_manager-0.3.4 lib/state_manager/adapters/base.rb
state_manager-0.3.3 lib/state_manager/adapters/base.rb
state_manager-0.3.2 lib/state_manager/adapters/base.rb
state_manager-0.3.1 lib/state_manager/adapters/base.rb
state_manager-0.3.0 lib/state_manager/adapters/base.rb
state_manager-0.2.13 lib/state_manager/adapters/base.rb
state_manager-0.2.12 lib/state_manager/adapters/base.rb
state_manager-0.2.11 lib/state_manager/adapters/base.rb
state_manager-0.2.10 lib/state_manager/adapters/base.rb
state_manager-0.2.9 lib/state_manager/adapters/base.rb
state_manager-0.2.8 lib/state_manager/adapters/base.rb
state_manager-0.2.7 lib/state_manager/adapters/base.rb
state_manager-0.2.6 lib/state_manager/adapters/base.rb
state_manager-0.2.5 lib/state_manager/adapters/base.rb
state_manager-0.2.4 lib/state_manager/adapters/base.rb
state_manager-0.2.3 lib/state_manager/adapters/base.rb