Sha256: 196c11dc5413f44c753f3190ecadd79e534668e6c3f71f1e3244d183ac229613

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

require 'active_support'
require 'active_record'
require 'uuid'

require 'active_support/dependencies/autoload'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/concern'

module Pseudocephalopod
  extend ActiveSupport::Autoload

  mattr_accessor :cache_key_prefix, :cache

  class << self

    def with_counter(prefix, counter = 0)
      counter < 1 ? prefix : "#{prefix}--#{counter}"
    end

    def next_value(scope, prefix)
      counter = 0
      slug    = self.with_counter(prefix, counter)
      while scope.with_cached_slug(slug).exists?
        counter += 1
        slug     = self.with_counter(prefix, counter)
      end
      slug
    end

    def uuid
      @uuid ||= UUID.new
    end

    def generate_uuid_slug
      uuid.generate
    end

    def last_known_slug_id(scope, slug)
      Pseudocephalopod::Slug.id_for(Pseudocephalopod.key_for_scope(scope), slug)
    end

    def record_slug(record, slug)
      Pseudocephalopod::Slug.record_slug(record, slug)
    end

    def previous_slugs_for(record)
      Pseudocephalopod::Slug.previous_for(record)
    end

    def remove_slug_history_for(record)
      Pseudocephalopod::Slug.remove_history_for(record)
    end

    def key_for_scope(scope)
      if scope.respond_to?(:slug_scope_key)
        scope.slug_scope_key
      elsif scope.class.respond_to?(:slug_scope_key)
        scope.class.slug_scope_key
      else
        scope.to_s
      end
    end

  end

  self.cache_key_prefix ||= "cached-slugs"

  autoload :Caching
  autoload :Scopes
  autoload :Finders
  autoload :SlugHistory
  autoload :Slug
  autoload :MemoryCache

  require 'pseudocephalopod/active_record_methods'
  ActiveRecord::Base.extend Pseudocephalopod::ActiveRecordMethods

  require 'pseudocephalopod/railtie' if defined?(Rails::Railtie)

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pseudocephalopod-0.3.1 lib/pseudocephalopod.rb
pseudocephalopod-0.3.0 lib/pseudocephalopod.rb
pseudocephalopod-0.2.5 lib/pseudocephalopod.rb