Sha256: b25465f457855bdedd2b5d9190de7f75d945182cbde08e1229b3358e847fcaa7

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 KB

Contents

module Quickery
  class MappingsBuilder
    attr_reader :model
    attr_reader :mappings

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

    # https://stackoverflow.com/questions/9647997/converting-a-nested-hash-into-a-flat-hash
    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

    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
quickery-1.3.1 lib/quickery/mappings_builder.rb
quickery-1.3.0 lib/quickery/mappings_builder.rb
quickery-1.2.0 lib/quickery/mappings_builder.rb
quickery-1.1.0 lib/quickery/mappings_builder.rb
quickery-1.0.0 lib/quickery/mappings_builder.rb