Sha256: ad35faf048f8287709df1b90ed76a2f5403f3fc158b1a98bb2c07834c3f88d2c

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

module CurriculumGenerator
  class Curriculum

    attr_accessor :langs, :master_lang, :data_loader, :compiler, :data_pth

    def initialize(data_loader, compiler, data_pth, template_pth, langs=[], master_lang=nil)
      # Preconditions
      raise 'Invalid langs. It cannot be empty' if langs.empty?

      @data_loader = data_loader
      @compiler = compiler
      @langs = {}
      # If the master language is not provided, then defaults to the first
      # provided language.
      @master_lang = master_lang.nil? ? langs[0] : master_lang

      @data_pth = data_pth
      @template_pth = template_pth

      puts "> Picking up the available languages.".green

      langs.each do |lang|
        lang_data_pth = @data_pth.join(lang.to_s)

        inst = self
        Either.chain do
          bind -> { lang_data_pth.directory? }
          bind -> {
            lang_data = inst.data_loader.load_data(
                inst.data_pth, lang.to_sym, inst.master_lang.to_sym)
            lang_data.is_a?(Hash) ? Success(lang_data) : Failure("lang data")
          }
          bind ->(lang_data) {
            inst.langs[lang.to_sym] = { data: lang_data }
          }
        end
      end
    end

    def validate_deps(template_deps_file_pth)
      @compiler.validate_deps template_deps_file_pth
    end

    # Compile the curriculum for the provided languages.
    def compile(langs)
      puts ">> Compiling the curriculum for the languages: " \
           "`#{langs.join(" ").light_black}`.".green

      # TODO: Refactor into monads, with better error detection.
      if langs.respond_to?(:each)
        langs.each do |lang|
          lang = lang.to_sym
          @compiler.compile(@langs[lang][:data], @template_pth, lang)
        end
      elsif @langs.include? langs
        @compiler.compile(@langs[lang][:data], @template_pth, langs)
      else
        raise "Invalid language."
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
curriculum-generator-1.0.7 lib/curriculum-generator/curriculum.rb
curriculum-generator-1.0.6 lib/curriculum-generator/curriculum.rb
curriculum-generator-1.0.5 lib/curriculum-generator/curriculum.rb