Sha256: e8da7e91607009b0501ba45d8fdfddb694211ad772adbef9dbc86f0bfe4139e4

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module MasterDataTool
  class MasterDataFileCollection
    def initialize(spec_name, override_identifier: nil)
      @spec_name = spec_name
      @override_identifier = override_identifier
      @collection = build

      freeze
    end

    def each
      return enum_for(:each) unless block_given?

      @collection.each do |file|
        yield file
      end
    end

    def to_a
      each.to_a
    end

    private

    def build
      files = extract_master_data_csv_paths.presence&.index_by(&:table_name)
      overridden_files = overridden_master_data_csv_paths.presence&.index_by(&:table_name) || {}

      table_names = (files.keys + overridden_files.keys).uniq
      table_names.map do |table_name|
        overridden_files[table_name] || files[table_name]
      end
    end

    def extract_master_data_csv_paths
      pattern = MasterDataTool.config.csv_dir_for(@spec_name).join('*.csv').to_s
      Pathname.glob(pattern).select(&:file?).map do |path|
        MasterDataFile.build(@spec_name, path, nil)
      end
    end

    def overridden_master_data_csv_paths
      return [] if @override_identifier.blank?

      pattern = MasterDataTool.config.csv_dir_for(@spec_name, @override_identifier).join('*.csv').to_s
      Pathname.glob(pattern).select(&:file?).map do |path|
        MasterDataFile.build(@spec_name, path, @override_identifier)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
master_data_tool-0.22.0 lib/master_data_tool/master_data_file_collection.rb
master_data_tool-0.21.1 lib/master_data_tool/master_data_file_collection.rb
master_data_tool-0.21.0 lib/master_data_tool/master_data_file_collection.rb
master_data_tool-0.20.0 lib/master_data_tool/master_data_file_collection.rb