Sha256: ea2327aa6b4207d141e4be288648d7be89eadd03f38a882ee98f9d68dca24e46

Contents?: true

Size: 1.03 KB

Versions: 77

Compression:

Stored size: 1.03 KB

Contents

class MockRecord
  IDS = Hash.new { |h, k| h[k] = 0 }
  QUERY_COUNTS = Hash.new { |h, k| h[k] = 0 }
  INSTANCES = Hash.new { |h, k| h[k] = {} }

  attr_reader :id

  class <<self
    def reset!
      IDS[name.to_sym] = 0
      INSTANCES[name.to_sym] = {}
    end
  end

  def initialize(attrs = {})
    @id = attrs.delete(:id) || IDS[self.class.name.to_sym] += 1
    INSTANCES[self.class.name.to_sym][@id] = self
    attrs.each_pair do |name, value|
      send(:"#{name}=", value)
    end
  end

  def self.inherited(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def get(id)
      QUERY_COUNTS[self.name.to_sym] += 1
      get_instance(id)
    end

    def get_all(ids)
      QUERY_COUNTS[self.name.to_sym] += 1
      ids.map { |id| get_instance(id) }.compact.sort_by { |instance| instance.id }
    end

    def query_count
      QUERY_COUNTS[self.name.to_sym]
    end

    private

    def get_instance(id)
      INSTANCES[self.name.to_sym][id]
    end
  end

  def destroy
    INSTANCES[self.class.name.to_sym].delete(@id)
  end
end

Version data entries

77 entries across 77 versions & 14 rubygems

Version Path
sunspot-2.7.1 spec/mocks/mock_record.rb
sunspot-2.7.0 spec/mocks/mock_record.rb
sunspot-2.6.0 spec/mocks/mock_record.rb
sunspot-2.5.0 spec/mocks/mock_record.rb
sunspot-2.4.0 spec/mocks/mock_record.rb
sunspot-2.3.0 spec/mocks/mock_record.rb
sunspot-2.2.8 spec/mocks/mock_record.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/sunspot-2.2.7/spec/mocks/mock_record.rb
sunspot-2.2.7 spec/mocks/mock_record.rb
sunspot-2.2.6 spec/mocks/mock_record.rb
sunspot-2.2.5 spec/mocks/mock_record.rb
sunspot-2.2.4 spec/mocks/mock_record.rb
sunspot-2.2.3 spec/mocks/mock_record.rb
sunspot-2.2.2 spec/mocks/mock_record.rb
sunspot-2.2.1 spec/mocks/mock_record.rb
sunspot-2.2.0 spec/mocks/mock_record.rb
sunspot-2.1.1 spec/mocks/mock_record.rb
sunspot_stats-0.0.7 spec/mocks/mock_record.rb
sunspot-2.1.0 spec/mocks/mock_record.rb
sunspot_stats-0.0.6 spec/mocks/mock_record.rb