Class: Quby::Compiler::Instance
- Inherits:
-
Object
- Object
- Quby::Compiler::Instance
- Defined in:
- lib/quby/compiler/instance.rb
Instance Attribute Summary collapse
-
#lookup_tables ⇒ Object
readonly
Returns the value of attribute lookup_tables.
Instance Method Summary collapse
- #compile(key:, sourcecode:, path: nil, translations: {}, &block) ⇒ Object
-
#initialize(lookup_tables:) ⇒ Instance
constructor
A new instance of Instance.
- #validate(key:, sourcecode:, translations: {}) ⇒ Object
Constructor Details
#initialize(lookup_tables:) ⇒ Instance
Returns a new instance of Instance.
8 9 10 |
# File 'lib/quby/compiler/instance.rb', line 8 def initialize(lookup_tables:) @lookup_tables = lookup_tables end |
Instance Attribute Details
#lookup_tables ⇒ Object (readonly)
Returns the value of attribute lookup_tables.
6 7 8 |
# File 'lib/quby/compiler/instance.rb', line 6 def lookup_tables @lookup_tables end |
Instance Method Details
#compile(key:, sourcecode:, path: nil, translations: {}, &block) ⇒ Object
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 64 65 66 67 68 69 70 |
# File 'lib/quby/compiler/instance.rb', line 12 def compile(key:, sourcecode:, path: nil, translations: {}, &block) questionnaire = \ if block # defined in block for tests DSL.build(key, path:, &block) elsif path # sourcecode given as string definition = Entities::Definition.new(key:, sourcecode:, path: path || "validating '#{key}'", lookup_tables:, translations:) DSL.build_from_definition(definition) # this will also validate the definition, and raise if it is invalid. We can rescue this in the caller to return the validation errors in a structured way. else tempfile = Tempfile.create([key, '.rb']) tempfile.write(sourcecode) tempfile.close definition = Entities::Definition.new(key:, sourcecode:, path: tempfile.path, lookup_tables:, translations:) DSL.build_from_definition(definition) # this will also validate the definition, and end original_locale = Outputs::LocaleSerializer.new(questionnaire).as_json all_translations = translations.clone \ .delete_if { |k, _| !questionnaire.translatable_into&.include?(k) } \ .merge(questionnaire.language => original_locale) { outputs: { definition: Output.new( key: :definition, filename: "definition.rb", content: sourcecode, ), roqua: Output.new( key: :roqua, filename: "roqua.json", content: Oj.dump(Outputs::RoquaSerializer.new(questionnaire).as_json, mode: :json, indent: 2), ), 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: Oj.dump(Outputs::QubyFrontendV1Serializer.new(questionnaire).as_json, mode: :json, indent: 2), ), quby_frontend_v2: Output.new( key: :quby_frontend_v2, filename: "quby-frontend-v2.json", content: Oj.dump(Outputs::QubyFrontendV2Serializer.new(questionnaire, translations: all_translations).as_json, mode: :json, indent: 2), ), locale: questionnaire.translatable?.presence && Output.new( key: :locale, filename: "#{LOCALE_FILE_PREFIX}#{questionnaire.language}.json", content: Oj.dump(original_locale, mode: :json, indent: 2), ), }.compact } 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 File.unlink(tempfile&.path) if tempfile end |
#validate(key:, sourcecode:, translations: {}) ⇒ Object
72 73 74 75 76 |
# File 'lib/quby/compiler/instance.rb', line 72 def validate(key:, sourcecode:, translations: {}) definition = Entities::Definition.new(key:, sourcecode:, path: "validating '#{key}'", lookup_tables:, translations:) definition.valid? definition end |