Sha256: c2d9bfe7b039ca43f2f5e52c6d0dd6d0c5f3a07f0c98cccbe7ef16d774665cdc

Contents?: true

Size: 1.14 KB

Versions: 13

Compression:

Stored size: 1.14 KB

Contents

module Toy
  module Indices
    extend ActiveSupport::Concern

    module ClassMethods
      def indices
        @indices ||= {}
      end

      def index(name)
        Index.new(self, name)
      end

      def index_key(name, value)
        if index = indices[name.to_sym]
          index.key(value)
        else
          raise(ArgumentError, "Index for #{name} does not exist")
        end
      end

      def get_index(name, value)
        key = index_key(name, value)
        store.read(key) || []
      end

      def create_index(name, value, id)
        key = index_key(name, value)
        ids = get_index(name, value)
        ids.push(id) unless ids.include?(id)
        store.write(key, ids)
      end

      def destroy_index(name, value, id)
        key = index_key(name, value)
        ids = get_index(name, value)
        ids.delete(id)
        store.write(key, ids)
      end
    end

    module InstanceMethods
      def indices
        self.class.indices
      end

      def create_index(*args)
        self.class.create_index(*args)
      end

      def destroy_index(*args)
        self.class.destroy_index(*args)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

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