Sha256: 2f82c55202e4e304da76c9fe4ee40309f770cfaee0a3f628bf16ecc06fc02aaf

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

require "yaml"


module CurriculumGenerator
  module DataLoader
    class YamlDataLoader

      # Load the localized data from the directory `data_dir_pth`, following the convention that the localized data for
      # a language are in a subdirectory of `data_dir_pth` named with the same name of the language.
      # The target language name (which is also the subdirectory name) is `trgt_lang`, which fallbacks to the `master_lang`
      def load_data(data_dir_pth, trgt_lang, master_lang)
        CurriculumGenerator::Util::Logging.log(:loading_curriculum_data, trgt_lang: trgt_lang, master_lang: master_lang)

        trgt_lang_data_dir_pth = data_dir_pth.join(trgt_lang.to_s)
        master_lang_data_dir_pth = data_dir_pth.join(master_lang.to_s)

        master_data = load_recursive_from_pth(trgt_lang_data_dir_pth)
        trgt_data = load_recursive_from_pth(master_lang_data_dir_pth)

        trgt_data.deep_merge(master_data)
      end

      # Load all of the YAML file starting from the given `base_dir_pth` and merges all of the data into an `Hash` and
      # returns it
      def load_recursive_from_pth(base_dir_pth)
        data = {}

        Dir.glob(base_dir_pth.join('**').join('*.yml')) do |yml_file_pth|
          File.open(yml_file_pth, 'r') { |yml_file| data.merge!(YAML::load(yml_file)) }
        end

        data
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
curriculum-generator-1.0.7 lib/curriculum-generator/data_loader/yaml_data_loader.rb
curriculum-generator-1.0.6 lib/curriculum-generator/data_loader/yaml_data_loader.rb