Class: CGen::DataLoader::YamlDataLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/cgen/data_loader/yaml_data_loader.rb

Instance Method Summary collapse

Instance Method Details

#load_data(data_dir_pth, trgt_lang, master_lang) ⇒ Object

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`



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cgen/data_loader/yaml_data_loader.rb', line 9

def load_data(data_dir_pth, trgt_lang, master_lang)
  CGen::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) # return
end

#load_recursive_from_pth(base_dir_pth) ⇒ Object

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



23
24
25
26
27
28
29
# File 'lib/cgen/data_loader/yaml_data_loader.rb', line 23

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