Class: Vorpal::Engine

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

Instance Method Summary collapse

Constructor Details

#initialize(db_driver, main_config) ⇒ Engine

Returns a new instance of Engine.



10
11
12
13
# File 'lib/vorpal/engine.rb', line 10

def initialize(db_driver, main_config)
  @db_driver = db_driver
  @configs = main_config
end

Instance Method Details

#class_config(domain_class) ⇒ Object



101
102
103
# File 'lib/vorpal/engine.rb', line 101

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

#db_class(domain_class) ⇒ Object

Try to use AggregateMapper#db_class instead.



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

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

#destroy(roots) ⇒ Object

Try to use AggregateMapper#destroy instead.



69
70
71
72
73
74
75
76
# File 'lib/vorpal/engine.rb', line 69

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.



79
80
81
82
83
84
85
86
87
88
# File 'lib/vorpal/engine.rb', line 79

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.



58
59
60
61
62
63
64
65
66
# File 'lib/vorpal/engine.rb', line 58

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.



53
54
55
# File 'lib/vorpal/engine.rb', line 53

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
48
49
50
# 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
    # Primary keys are set eagerly (instead of waiting for them to be set by ActiveRecord upon create)
    # because we want to support non-null FK constraints without needing to figure the correct
    # order to save entities in.
    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

Try to use AggregateMapper#query instead.



96
97
98
# File 'lib/vorpal/engine.rb', line 96

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