Class: CleanArchitecture::Builders::AbstractActiveRecordEntityBuilder

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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.



47
48
49
# File 'lib/clean_architecture/builders/abstract_active_record_entity_builder.rb', line 47

def initialize(ar_model_instance)
  @ar_model_instance = ar_model_instance
end

Class Method Details

.acts_as_builder_for_entity(entity_class) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/clean_architecture/builders/abstract_active_record_entity_builder.rb', line 15

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



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

def self.belongs_to(relation_name, use:)
  @belongs_to_builders = T.let(@belongs_to_builders, T.nilable(T::Array[Object]))
  T.must(@belongs_to_builders) << [relation_name, use]
end

.has_many(relation_name, use:) ⇒ Object



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

def self.has_many(relation_name, use:)
  @has_many_builders = T.let(@has_many_builders, T.nilable(T::Array[Object]))
  T.must(@has_many_builders) << [relation_name, use]
end

Instance Method Details

#buildObject



52
53
54
# File 'lib/clean_architecture/builders/abstract_active_record_entity_builder.rb', line 52

def build
  specified_entity_class.new(all_attributes_for_entity)
end

#specified_entity_classObject



57
58
59
# File 'lib/clean_architecture/builders/abstract_active_record_entity_builder.rb', line 57

def specified_entity_class
  send(:entity_class)
end