Class: Vorpal::AggregateMapper

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

Instance Method Summary collapse

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_classObject

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.

Parameters:

  • roots ([Object])

    Roots of the aggregates to be destroyed. Also accepts a single root.

Returns:

  • ([Object])

    Roots that were passed in.

Raises:



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.

Parameters:

  • ids ([Integer])

    Ids of roots of the aggregates to be destroyed. Also accepts a single id.

Raises:



69
70
71
# File 'lib/vorpal/aggregate_mapper.rb', line 69

def destroy_by_id(ids)
  @engine.destroy_by_id(ids, @domain_class)
end

#engineEngine

Access to the underlying mapping Engine. Provided in case access to another aggregate or another db_class is required.

Returns:

  • (Engine)

    Mapping interface not specific to a particular aggregate root.



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.

Parameters:

  • db_roots ([Integer])

    Array of primary key values of the roots of the aggregates to be loaded.

  • identity_map (IdentityMap) (defaults to: IdentityMap.new)

    Provide your own IdentityMap instance if you want entity id -> unique object mapping for a greater scope than one operation.

Returns:

  • ([Object])

    Aggregate roots corresponding to the given DB representations.

Raises:



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).

Parameters:

  • db_root (Object, nil)

    DB representation of the root of the aggregate to be loaded. This can be nil.

  • identity_map (IdentityMap) (defaults to: IdentityMap.new)

    Provide your own IdentityMap instance if you want entity id -> unique object mapping for a greater scope than one operation.

Returns:

  • (Object, nil)

    Aggregate root corresponding to the given DB representation.



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.

Parameters:

  • roots ([Object])

    array of aggregate roots to be saved. Will also accept a single aggregate.

Returns:

  • ([Object])

    array of aggregate roots.

Raises:



23
24
25
# File 'lib/vorpal/aggregate_mapper.rb', line 23

def persist(roots)
  @engine.persist(roots)
end

#queryActiveRecord::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.

Returns:

  • (ActiveRecord::Relation)


98
99
100
# File 'lib/vorpal/aggregate_mapper.rb', line 98

def query
  @engine.query(@domain_class)
end