Sha256: d31d78a0bcfc87c7a62feb7146f21582596b80874fd2ec560c79c5a99af19e6e

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module MemoryModel
  class Collection
    module Initializers
      extend ActiveSupport::Concern

      def initialize(model)
        @model = model
        set_primary_key :_uuid_, default: nil
      end

      def add_index(name, options={})
        type          = :unique if options.delete(:unique)
        type          ||= options.delete(:type) || :multi
        indexes[name] = Index.const_get(type.to_s.camelize).new(name, options)
      rescue NameError => e
        raise TypeError, "#{type.inspect} is not a valid index"
      end

      def indexes
        @indexes ||= {}
      end

      def index_names
        indexes.keys
      end

      def set_primary_key(key, options={})
        if options[:auto_increment] != false && !options.has_key?(:default)
          options[:auto_increment] = true
        end
        options[:comparable] ||= false
        @model.field key, options
        add_index key, type: :unique
        @primary_key = key
      end

      module ClassMethods

        def all
          MemoryModel::Collection.instance_variable_get(:@all) ||
            MemoryModel::Collection.instance_variable_set(:@all, [])
        end

      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
memory_model-1.0.0 lib/memory_model/collection/initializers.rb
memory_model-0.1.0 lib/memory_model/collection/initializers.rb