Class: CleanArchitecture::Builders::AbstractActiveRecordEntityBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/clean_architecture/builders/abstract_active_record_entity_builder.rb

Overview

Helps to take an instance of an AR model and wrap it up in the given Entity Any columns from the AR model that do not directly map to an attribute on the Entity can be specified by overriding #attributes_for_entity.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ar_model_instance) ⇒ AbstractActiveRecordEntityBuilder

Returns a new instance of AbstractActiveRecordEntityBuilder.

Parameters:

  • An (ActiveRecord::Base)

    ActiveRecord model to map to the entity



39
40
41
# File 'lib/clean_architecture/builders/abstract_active_record_entity_builder.rb', line 39

def initialize(ar_model_instance)
  @ar_model_instance = ar_model_instance
end

Class Method Details

.acts_as_builder_for_entity(entity_class) ⇒ Object

Parameters:

  • A (Class)

    Dry::Struct based entity that this builder will construct instances of



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/clean_architecture/builders/abstract_active_record_entity_builder.rb', line 11

def self.acts_as_builder_for_entity(entity_class)
  @has_many_builders = []
  @belongs_to_builders = []

  define_singleton_method :has_many_builders do
    @has_many_builders
  end

  define_singleton_method :belongs_to_builders do
    @belongs_to_builders
  end

  define_method :entity_class do
    entity_class
  end

  private :entity_class
end

.belongs_to(relation_name, use:) ⇒ Object



34
35
36
# File 'lib/clean_architecture/builders/abstract_active_record_entity_builder.rb', line 34

def self.belongs_to(relation_name, use:)
  @belongs_to_builders << [relation_name, use]
end

.has_many(relation_name, use:) ⇒ Object



30
31
32
# File 'lib/clean_architecture/builders/abstract_active_record_entity_builder.rb', line 30

def self.has_many(relation_name, use:)
  @has_many_builders << [relation_name, use]
end

Instance Method Details

#buildObject



43
44
45
# File 'lib/clean_architecture/builders/abstract_active_record_entity_builder.rb', line 43

def build
  entity_class.new(all_attributes_for_entity)
end