Class: Quby::Compiler::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/quby/compiler/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookup_tables:) ⇒ Instance

Returns a new instance of Instance.



6
7
8
# File 'lib/quby/compiler/instance.rb', line 6

def initialize(lookup_tables:)
  @lookup_tables = lookup_tables
end

Instance Attribute Details

#lookup_tablesObject (readonly)

Returns the value of attribute lookup_tables.



4
5
6
# File 'lib/quby/compiler/instance.rb', line 4

def lookup_tables
  @lookup_tables
end

Instance Method Details

#compile(key:, sourcecode:, path: nil, &block) ⇒ Object



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/quby/compiler/instance.rb', line 10

def compile(key:, sourcecode:, path: nil, &block)
  if block # defined in block for tests
    questionnaire = DSL.build(key, path: path, &block)
  else # sourcecode given as string
    tempfile = Tempfile.new([key, '.rb'])
    questionnaire = Entities::Questionnaire.new(key)
    Thread.current["quby-questionnaire-loading"] = Quby::Compiler::DSL::QuestionnaireBuilder.new(questionnaire, lookup_tables: lookup_tables)

    tempfile.puts "Thread.current['quby-questionnaire-loading'].instance_eval do"
    tempfile.puts sourcecode
    tempfile.puts "end"
    tempfile.flush

    load tempfile.path
    Thread.current['quby-questionnaire-loading'] = nil

    questionnaire.callback_after_dsl_enhance_on_questions
  end

  {
    outputs: {
      definition: Output.new(
        key: :definition,
        filename: "definition.rb",
        content: sourcecode,
      ),
      roqua: Output.new(
        key: :roqua,
        filename: "roqua.json",
        content: Outputs::RoquaSerializer.new(questionnaire).to_json,
      ),
      seeds: Output.new(
        key: :seeds,
        filename: "seeds.yml",
        content: YAML.dump(Outputs::SeedSerializer.new(questionnaire).generate),
      ),
      quby_frontend_v1: Output.new(
        key: :quby_frontend_v1,
        filename: "quby-frontend-v1.json",
        content: Outputs::QubyFrontendV1Serializer.new(questionnaire).to_json,
      ),
      quby_frontend_v2: Output.new(
        key: :quby_frontend_v2,
        filename: "quby-frontend-v2.json",
        content: Outputs::QubyFrontendV2Serializer.new(questionnaire).to_json,
      ),
    },
  }
ensure
  # We can only close and remove the file once serializers have finished.
  # The serializers need the file in order to grab the source for score blocks
  tempfile&.close
  tempfile&.unlink
end

#validate(key:, sourcecode:) ⇒ Object



65
66
67
68
69
# File 'lib/quby/compiler/instance.rb', line 65

def validate(key:, sourcecode:)
  definition = Entities::Definition.new(key: key, sourcecode: sourcecode, path: "validating '#{key}'", lookup_tables: lookup_tables)
  definition.valid?
  definition
end