Class: CGen::Curriculum

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_loader, compiler, data_pth, template_pth, langs = [], master_lang = nil) ⇒ Curriculum

Returns a new instance of Curriculum.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cgen/curriculum.rb', line 5

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

Instance Attribute Details

#compilerObject

Returns the value of attribute compiler.



3
4
5
# File 'lib/cgen/curriculum.rb', line 3

def compiler
  @compiler
end

#data_loaderObject

Returns the value of attribute data_loader.



3
4
5
# File 'lib/cgen/curriculum.rb', line 3

def data_loader
  @data_loader
end

#data_pthObject

Returns the value of attribute data_pth.



3
4
5
# File 'lib/cgen/curriculum.rb', line 3

def data_pth
  @data_pth
end

#langsObject

Returns the value of attribute langs.



3
4
5
# File 'lib/cgen/curriculum.rb', line 3

def langs
  @langs
end

#master_langObject

Returns the value of attribute master_lang.



3
4
5
# File 'lib/cgen/curriculum.rb', line 3

def master_lang
  @master_lang
end

Instance Method Details

#compile(langs) ⇒ Object

Compile the curriculum for the provided languages



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cgen/curriculum.rb', line 42

def compile(langs)
  puts '>> Compiling the curriculum for the languages: '.green + langs.join(' ').light_black

  # 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 lang'
  end
end

#validate_deps(template_deps_file_pth) ⇒ Object



37
38
39
# File 'lib/cgen/curriculum.rb', line 37

def validate_deps(template_deps_file_pth)
  @compiler.validate_deps template_deps_file_pth
end