Class: Vorpal::AggregateMapper
- Inherits:
-
Object
- Object
- Vorpal::AggregateMapper
- Defined in:
- lib/vorpal/aggregate_mapper.rb
Instance Method Summary collapse
-
#db_class ⇒ Object
Returns the DB Class (e.g. ActiveRecord::Base class) that is responsible for accessing the associated data in the DB.
-
#destroy(roots) ⇒ [Object]
Removes a collection of aggregates from the DB.
-
#destroy_by_id(ids) ⇒ Object
Removes a collection of aggregates from the DB given their primary keys.
-
#engine ⇒ Engine
Access to the underlying mapping Engine.
-
#initialize(domain_class, engine) ⇒ AggregateMapper
constructor
A new instance of AggregateMapper.
-
#load_many(db_roots, identity_map = IdentityMap.new) ⇒ [Object]
Like #load_one but operates on multiple aggregate roots.
-
#load_one(db_root, identity_map = IdentityMap.new) ⇒ Object?
Loads an aggregate from the DB.
-
#persist(roots) ⇒ [Object]
Saves a collection of aggregates to the DB.
-
#query ⇒ ActiveRecord::Relation
Returns a ‘Vorpal-aware’ [ActiveRecord::Relation](api.rubyonrails.org/classes/ActiveRecord/Relation.html) for the ActiveRecord object underlying the domain entity mapped by this mapper.
Constructor Details
#initialize(domain_class, engine) ⇒ AggregateMapper
Returns a new instance of AggregateMapper.
6 7 8 9 |
# File 'lib/vorpal/aggregate_mapper.rb', line 6 def initialize(domain_class, engine) @domain_class = domain_class @engine = engine end |
Instance Method Details
#db_class ⇒ Object
Returns the DB Class (e.g. ActiveRecord::Base class) that is responsible for accessing the associated data in the DB.
75 76 77 |
# File 'lib/vorpal/aggregate_mapper.rb', line 75 def db_class @engine.db_class(@domain_class) end |
#destroy(roots) ⇒ [Object]
Removes a collection of aggregates from the DB. Even if an aggregate contains unsaved changes this method will correctly remove everything.
60 61 62 |
# File 'lib/vorpal/aggregate_mapper.rb', line 60 def destroy(roots) @engine.destroy(roots) end |
#destroy_by_id(ids) ⇒ Object
Removes a collection of aggregates from the DB given their primary keys.
69 70 71 |
# File 'lib/vorpal/aggregate_mapper.rb', line 69 def destroy_by_id(ids) @engine.destroy_by_id(ids, @domain_class) end |
#engine ⇒ Engine
Access to the underlying mapping Engine. Provided in case access to another aggregate or another db_class is required.
83 84 85 |
# File 'lib/vorpal/aggregate_mapper.rb', line 83 def engine @engine end |
#load_many(db_roots, identity_map = IdentityMap.new) ⇒ [Object]
Like #load_one but operates on multiple aggregate roots.
49 50 51 |
# File 'lib/vorpal/aggregate_mapper.rb', line 49 def load_many(db_roots, identity_map=IdentityMap.new) @engine.load_many(db_roots, @domain_class, identity_map) end |
#load_one(db_root, identity_map = IdentityMap.new) ⇒ Object?
Loads an aggregate from the DB. Will eagerly load all objects in the aggregate and on the boundary (owned: false).
36 37 38 |
# File 'lib/vorpal/aggregate_mapper.rb', line 36 def load_one(db_root, identity_map=IdentityMap.new) @engine.load_one(db_root, @domain_class, identity_map) end |
#persist(roots) ⇒ [Object]
Saves a collection of aggregates to the DB. Inserts objects that are new to an aggregate, updates existing objects and deletes objects that are no longer present.
Objects that are on the boundary of an aggregate (owned: false) will not be inserted, updated, or deleted. However, the relationships to these objects (provided they are stored within an aggregate) will be saved.
23 24 25 |
# File 'lib/vorpal/aggregate_mapper.rb', line 23 def persist(roots) @engine.persist(roots) end |
#query ⇒ ActiveRecord::Relation
Returns a ‘Vorpal-aware’ [ActiveRecord::Relation](api.rubyonrails.org/classes/ActiveRecord/Relation.html) for the ActiveRecord object underlying the domain entity mapped by this mapper.
This method allows you to easily access the power of ActiveRecord::Relation to do more complex queries in your repositories.
The ActiveRecord::Relation is ‘Vorpal-aware’ because it has the #load_one and #load_many methods mixed in so that you can get the POROs from your domain model instead of the ActiveRecord objects normally returned by ActiveRecord::Relation.
98 99 100 |
# File 'lib/vorpal/aggregate_mapper.rb', line 98 def query @engine.query(@domain_class) end |