Module: ORMivore::MemoryAdapter

Included in:
App::AccountStorageMemoryAdapter, App::AddressStorageMemoryAdapter
Defined in:
lib/ormivore/memory_adapter.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
# File 'lib/ormivore/memory_adapter.rb', line 10

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

Instance Method Details

#create(attrs) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ormivore/memory_adapter.rb', line 27

def create(attrs)
  id = attrs[:id]
  if id
    raise RecordAlreadyExists if storage.any? { |o| o[:id] == id }
  else
    id = next_id
  end
  attrs.merge(id: id).tap { |attrs_with_id|
    storage << attrs_with_id
  }
end

#find(conditions, attributes_to_load, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/ormivore/memory_adapter.rb', line 18

def find(conditions, attributes_to_load, options = {})
  order = options.fetch(:order, {})

  reorder(
    filter_from_storage(conditions, attributes_to_load),
    order
  )
end

#initialize(converter = nil) ⇒ Object



14
15
16
# File 'lib/ormivore/memory_adapter.rb', line 14

def initialize(converter = nil)
  @converter = converter || self.class.default_converter_class.new
end

#storageObject

open for tests, not to be used by any other code



46
47
48
# File 'lib/ormivore/memory_adapter.rb', line 46

def storage
  @storage ||= []
end

#update(attrs, conditions) ⇒ Object



39
40
41
42
43
# File 'lib/ormivore/memory_adapter.rb', line 39

def update(attrs, conditions)
  select_from_storage(conditions).each { |record|
    record.merge!(attrs)
  }.length
end