Module: MemoryModel::Collection::Finders

Included in:
MemoryModel::Collection
Defined in:
lib/memory_model/collection/finders.rb

Instance Method Summary collapse

Instance Method Details

#allObject



9
10
11
# File 'lib/memory_model/collection/finders.rb', line 9

def all
  LoaderDelegate.new records
end

#countObject



13
14
15
# File 'lib/memory_model/collection/finders.rb', line 13

def count
  _uuids_.count
end

#find(key) ⇒ Object



17
18
19
20
21
# File 'lib/memory_model/collection/finders.rb', line 17

def find(key)
  read(key).load
rescue NoMethodError
  raise RecordNotFoundError
end

#find_all(*ids) ⇒ Object



23
24
25
# File 'lib/memory_model/collection/finders.rb', line 23

def find_all(*ids)
  read_all(*ids).map(&:load)
end

#find_by(hash) ⇒ Object



27
28
29
# File 'lib/memory_model/collection/finders.rb', line 27

def find_by(hash)
  where(hash).first
end

#find_or_create_by(hash) ⇒ Object



35
36
37
# File 'lib/memory_model/collection/finders.rb', line 35

def find_or_create_by(hash)
  find_by(hash) || model.create(hash)
end

#find_or_create_by!(hash) ⇒ Object



39
40
41
# File 'lib/memory_model/collection/finders.rb', line 39

def find_or_create_by!(hash)
  find_by(hash) || model.create!(hash)
end

#find_or_initialize_by(hash) ⇒ Object



31
32
33
# File 'lib/memory_model/collection/finders.rb', line 31

def find_or_initialize_by(hash)
  find_by(hash) || model.new(hash)
end

#where(hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/memory_model/collection/finders.rb', line 43

def where(hash)
  matched_ids = hash.symbolize_keys.reduce(_uuids_) do |array, (attr, value)|
    records = if indexes.has_key?(attr)
                where_in_index(attr, value).compact.map(&:uuid)
              else
                where_in_all(attr, value).map(&:_uuid_)
              end
    array & records
  end
  load_all(*matched_ids)
end