class FrozenRecord::Index

Constants

AttributeNonUnique
EMPTY_ARRAY

Attributes

attribute[R]
model[R]

Public Class Methods

new(model, attribute, unique: false) click to toggle source
# File lib/frozen_record/index.rb, line 12
def initialize(model, attribute, unique: false)
  @model = model
  @attribute = -attribute.to_s
  @index = nil
end

Public Instance Methods

build(records) click to toggle source
# File lib/frozen_record/index.rb, line 42
def build(records)
  @index = records.each_with_object({}) do |record, index|
    entry = (index[record[attribute]] ||= [])
    entry << record
  end
  @index.values.each(&:freeze)
  @index.freeze
end
lookup(value) click to toggle source
# File lib/frozen_record/index.rb, line 34
def lookup(value)
  @index.fetch(value, EMPTY_ARRAY)
end
lookup_multi(values) click to toggle source
# File lib/frozen_record/index.rb, line 30
def lookup_multi(values)
  values.flat_map { |v| lookup(v) }
end
query(matcher) click to toggle source
# File lib/frozen_record/index.rb, line 22
def query(matcher)
  if matcher.ranged?
    lookup_multi(matcher.value)
  else
    lookup(matcher.value)
  end
end
reset() click to toggle source
# File lib/frozen_record/index.rb, line 38
def reset
  @index = nil
end
unique?() click to toggle source
# File lib/frozen_record/index.rb, line 18
def unique?
  false
end