Sha256: 336d529d7e69bcb3b3e7a4207d5ec66142e6f5b6b598ba7e55d51c9670242b2e

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require_relative 'relation_dumper'

module EvilSeed
  # This module collects dumps generation for root and all it's dependencies
  class RootDumper
    attr_reader :root, :dumper, :model_class, :total_limit, :association_limits

    delegate :loaded_map, :configuration, to: :dumper

    def initialize(root, dumper)
      @root   = root
      @dumper = dumper
      @to_load_map = {}
      @total_limit = root.total_limit
      @association_limits = root.association_limits.dup

      @model_class = root.model.constantize
    end

    # Generate dump and write it into +io+
    # @param output [IO] Stream to write SQL dump into
    def call
      association_path = model_class.model_name.singular
      RelationDumper.new(model_class.where(*root.constraints), self, association_path).call
    end

    # @return [Boolean] +true+ if limits are NOT reached and +false+ otherwise
    def check_limits!(association_path)
      check_total_limit! && check_association_limits!(association_path)
    end

    private

    def check_total_limit!
      return true  if total_limit.nil?
      return false if total_limit.zero?
      @total_limit -= 1
      true
    end

    def check_association_limits!(association_path)
      return true if association_limits.none?
      applied_limits = association_limits.select { |path, _limit| path.match(association_path) }
      return false if applied_limits.any? { |_path, limit| limit.zero? }
      applied_limits.each do |path, _limit|
        association_limits[path] -= 1
      end
      true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
evil-seed-0.1.0 lib/evil_seed/root_dumper.rb