Class: Mxrb::Runtime::Native::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/runtime/native.rb

Overview

Transactional in-memory persistence used by the Ruby model Runtime.

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



15
16
17
# File 'lib/mxrb/runtime/native.rb', line 15

def initialize
  @records = Hash.new { |records, entity| records[entity] = [] }
end

Instance Method Details

#count(entity, predicate = nil) ⇒ Object



31
32
33
34
# File 'lib/mxrb/runtime/native.rb', line 31

def count(entity, predicate = nil)
  values = retrieve(entity)
  predicate ? values.count { predicate.call(_1) } : values.size
end

#create(entity) ⇒ Object



19
20
21
22
23
# File 'lib/mxrb/runtime/native.rb', line 19

def create(entity)
  ObjectValue.new(entity:, id: SecureRandom.uuid, members: {}).tap do |object|
    @records[entity] << object
  end
end

#delete(value) ⇒ Object



27
28
29
# File 'lib/mxrb/runtime/native.rb', line 27

def delete(value)
  Array(value).each { @records[_1.entity].delete(_1) }
end

#restore(snapshot) ⇒ Object



42
43
44
45
# File 'lib/mxrb/runtime/native.rb', line 42

def restore(snapshot)
  @records.clear
  snapshot.each { |entity, values| @records[entity] = values }
end

#retrieve(entity) ⇒ Object



25
# File 'lib/mxrb/runtime/native.rb', line 25

def retrieve(entity) = @records[entity].dup

#snapshotObject



36
37
38
39
40
# File 'lib/mxrb/runtime/native.rb', line 36

def snapshot
  @records.to_h do |entity, values|
    [entity, values.map { ObjectValue.new(entity: _1.entity, id: _1.id, members: _1.members.dup) }]
  end
end