Sha256: 4956826fbdf36eab3908ab9cb822273f647ab2d080f604cf2b33edd1e0bf1d93

Contents?: true

Size: 1.77 KB

Versions: 13

Compression:

Stored size: 1.77 KB

Contents

module Toy
  class Index
    attr_accessor :model, :name

    def initialize(model, name)
      @model, @name = model, name.to_sym
      raise(ArgumentError, "No attribute #{name} for index") unless model.attribute?(name)

      model.indices[name] = self
      model.send(:include, IndexCallbacks)
      create_finders
    end

    def eql?(other)
      self.class.eql?(other.class) &&
        model == other.model &&
        name  == other.name
    end
    alias :== :eql?

    def key(value)
      sha_value = Digest::SHA1.hexdigest(Array.wrap(value).sort.join('')) # sorted for predictability
      [model.name, name, sha_value].join(':')
    end

    module IndexCallbacks
      extend ActiveSupport::Concern

      included do
        after_create  :index_create
        after_update  :index_update
        after_destroy :index_destroy
      end

      def index_create
        indices.each_key do |name|
          create_index(name, send(name), id)
        end
      end

      def index_update
        indices.each_key do |name|
          if send(:"#{name}_changed?")
            destroy_index(name, send(:"#{name}_was"), id)
            create_index(name, send(name), id)
          end
        end
      end

      def index_destroy
        indices.each_key do |name|
          destroy_index(name, send(name), id)
        end
      end
    end

    private
      def create_finders
        model.class_eval """
          def self.first_by_#{name}(value)
            get(get_index(:#{name}, value)[0])
          end

          def self.first_or_new_by_#{name}(value)
            first_by_#{name}(value) || new(:#{name} => value)
          end

          def self.first_or_create_by_#{name}(value)
            first_by_#{name}(value) || create(:#{name} => value)
          end
        """
      end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
toystore-0.8.3 lib/toy/index.rb
toystore-0.8.2 lib/toy/index.rb
toystore-0.8.1 lib/toy/index.rb
toystore-0.8.0 lib/toy/index.rb
toystore-0.7.0 lib/toy/index.rb
toystore-0.6.6 lib/toy/index.rb
toystore-0.6.5 lib/toy/index.rb
toystore-0.6.4 lib/toy/index.rb
toystore-0.6.3 lib/toy/index.rb
toystore-0.6.2 lib/toy/index.rb
toystore-0.6.1 lib/toy/index.rb
toystore-0.6 lib/toy/index.rb
toystore-0.5 lib/toy/index.rb