Class: Quickery::MappingsBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/quickery/mappings_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, mappings:) ⇒ MappingsBuilder

Returns a new instance of MappingsBuilder.



6
7
8
9
# File 'lib/quickery/mappings_builder.rb', line 6

def initialize(model:, mappings:)
  @model = model
  @mappings = mappings
end

Instance Attribute Details

#mappingsObject (readonly)

Returns the value of attribute mappings.



4
5
6
# File 'lib/quickery/mappings_builder.rb', line 4

def mappings
  @mappings
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/quickery/mappings_builder.rb', line 3

def model
  @model
end

Instance Method Details

#flat_hash(hash, k = []) ⇒ Object



12
13
14
15
# File 'lib/quickery/mappings_builder.rb', line 12

def flat_hash(hash, k = [])
  return {k => hash} unless hash.is_a?(Hash)
  hash.inject({}){ |h, v| h.merge! flat_hash(v[-1], k + [v[0]]) }
end

#map_attributesObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/quickery/mappings_builder.rb', line 17

def map_attributes
  flat_hash(@mappings).each do |names, depender_column_name|
    first_association_chain = AssociationChain.new(model: model)
    first_association_chain.build_children_association_chains(names_left: names)

    all_association_chains = first_association_chain.child_association_chains(include_self: true)
    dependee_column_name = all_association_chains.last.dependee_column_name

    quickery_builder = QuickeryBuilder.new(
      model: @model,
      association_chains: all_association_chains,
      dependee_column_name: dependee_column_name,
      depender_column_name: depender_column_name,
    )

    quickery_builder.add_to_model
    quickery_builder.add_to_association_chains
    quickery_builder.create_model_callbacks
  end
end