Class: Vorpal::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/vorpal/engine.rb

Instance Method Summary collapse

Instance Method Details

#db_class(domain_class) ⇒ Object

Try to use AggregateMapper#db_class instead.



88
89
90
# File 'lib/vorpal/engine.rb', line 88

def db_class(domain_class)
  @configs.config_for(domain_class).db_class
end

#destroy(roots) ⇒ Object

Try to use AggregateMapper#destroy instead.



66
67
68
69
70
71
72
73
# File 'lib/vorpal/engine.rb', line 66

def destroy(roots)
  roots = wrap(roots)
  return roots if roots.empty?
  raise InvalidAggregateRoot, 'Nil aggregate roots are not allowed.' if roots.any?(&:nil?)

  destroy_by_id(roots.map(&:id), roots.first.class)
  roots
end

#destroy_by_id(ids, domain_class) ⇒ Object

Try to use AggregateMapper#destroy_by_id instead.



76
77
78
79
80
81
82
83
84
85
# File 'lib/vorpal/engine.rb', line 76

def destroy_by_id(ids, domain_class)
  ids = wrap(ids)
  raise InvalidPrimaryKeyValue, 'Nil primary key values are not allowed.' if ids.any?(&:nil?)

  loaded_db_objects = load_owned_from_db(ids, domain_class)
  loaded_db_objects.each do |config, db_objects|
    @db_driver.destroy(config.db_class, db_objects.map(&:id))
  end
  ids
end

#load_many(db_roots, domain_class, identity_map) ⇒ Object

Try to use AggregateMapper#load_many instead.



55
56
57
58
59
60
61
62
63
# File 'lib/vorpal/engine.rb', line 55

def load_many(db_roots, domain_class, identity_map)
  raise InvalidAggregateRoot, 'Nil aggregate roots are not allowed.' if db_roots.any?(&:nil?)

  loaded_db_objects = DbLoader.new(false, @db_driver).load_from_db_objects(db_roots, @configs.config_for(domain_class))
  deserialize(loaded_db_objects, identity_map)
  set_associations(loaded_db_objects, identity_map)

  db_roots.map { |db_object| identity_map.get(db_object) }
end

#load_one(db_root, domain_class, identity_map) ⇒ Object

Try to use AggregateMapper#load_one instead.



50
51
52
# File 'lib/vorpal/engine.rb', line 50

def load_one(db_root, domain_class, identity_map)
  load_many(Array(db_root), domain_class, identity_map).first
end

#mapper_for(domain_class) ⇒ AggregateMapper

Creates a mapper for saving/updating/loading/destroying an aggregate to/from the DB. It is possible to use the methods directly on the Vorpal::Engine.

Parameters:

  • domain_class (Class)

    Class of the root of the aggregate.

Returns:



20
21
22
# File 'lib/vorpal/engine.rb', line 20

def mapper_for(domain_class)
  AggregateMapper.new(domain_class, self)
end

#persist(roots) ⇒ Object

Try to use AggregateMapper#persist instead.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/vorpal/engine.rb', line 25

def persist(roots)
  roots = wrap(roots)
  return roots if roots.empty?
  raise InvalidAggregateRoot, 'Nil aggregate roots are not allowed.' if roots.any?(&:nil?)

  all_owned_objects = all_owned_objects(roots)
  mapping = {}
  loaded_db_objects = load_owned_from_db(roots.map(&:id).compact, roots.first.class)

  serialize(all_owned_objects, mapping, loaded_db_objects)
  new_objects = get_unsaved_objects(mapping.keys)
  begin
    set_primary_keys(all_owned_objects, mapping)
    set_foreign_keys(all_owned_objects, mapping)
    remove_orphans(mapping, loaded_db_objects)
    save(all_owned_objects, new_objects, mapping)

    return roots
  rescue Exception
    nil_out_object_ids(new_objects)
    raise
  end
end

#query(domain_class) ⇒ Object



92
93
94
# File 'lib/vorpal/engine.rb', line 92

def query(domain_class)
  @db_driver.query(@configs.config_for(domain_class).db_class, mapper_for(domain_class))
end