Class: FrozenRecord::UniqueIndex

Inherits:
Index
  • Object
show all
Defined in:
lib/frozen_record/index.rb

Constant Summary

Constants inherited from Index

Index::AttributeNonUnique

Instance Attribute Summary

Attributes inherited from Index

#attribute, #model

Instance Method Summary collapse

Methods inherited from Index

#initialize, #query, #reset

Constructor Details

This class inherits a constructor from FrozenRecord::Index

Instance Method Details

#build(records) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/frozen_record/index.rb', line 68

def build(records)
  @index = records.each_with_object({}) { |r, index| index[r[attribute]] = r }
  if @index.size != records.size
    raise AttributeNonUnique, "#{model}##{attribute.inspect} is not unique."
  end
  @index.freeze
end

#lookup(value) ⇒ Object



63
64
65
66
# File 'lib/frozen_record/index.rb', line 63

def lookup(value)
  record = @index[value]
  record ? [record] : EMPTY_ARRAY
end

#lookup_multi(values) ⇒ Object



57
58
59
60
61
# File 'lib/frozen_record/index.rb', line 57

def lookup_multi(values)
  results = @index.values_at(*values)
  results.compact!
  results
end

#unique?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/frozen_record/index.rb', line 53

def unique?
  true
end