Module: Kumi::Schema
- Included in:
- TestSharedSchemas::Compound, TestSharedSchemas::Discount, TestSharedSchemas::Price, TestSharedSchemas::Subtotal, TestSharedSchemas::Tax
- Defined in:
- lib/kumi/schema.rb
Overview
This module is the main entry point for users of the Kumi DSL. When a user module ‘extend`s this, it gains the `schema` block method and the `from` class method to execute the compiled logic.
Instance Attribute Summary collapse
-
#__kumi_compiled_module__ ⇒ Object
readonly
The ‘syntax_tree` is available on the class for introspection.
-
#__kumi_syntax_tree__ ⇒ Object
(also: #__syntax_tree__)
readonly
The ‘syntax_tree` is available on the class for introspection.
Instance Method Summary collapse
- #build_syntax_tree ⇒ Object
- #from(input_data) ⇒ Object
- #runner ⇒ Object
- #schema(&block) ⇒ Object
- #schema_metadata ⇒ Object
- #write_source(file_path, platform: :ruby) ⇒ Object
Instance Attribute Details
#__kumi_compiled_module__ ⇒ Object (readonly)
The ‘syntax_tree` is available on the class for introspection.
45 46 47 |
# File 'lib/kumi/schema.rb', line 45 def __kumi_compiled_module__ @__kumi_compiled_module__ end |
#__kumi_syntax_tree__ ⇒ Object (readonly) Also known as: __syntax_tree__
The ‘syntax_tree` is available on the class for introspection.
45 46 47 |
# File 'lib/kumi/schema.rb', line 45 def __kumi_syntax_tree__ @__kumi_syntax_tree__ end |
Instance Method Details
#build_syntax_tree ⇒ Object
48 49 50 |
# File 'lib/kumi/schema.rb', line 48 def build_syntax_tree(&) @__kumi_syntax_tree__ = Kumi::Core::RubyParser::Dsl.build_syntax_tree(&) end |
#from(input_data) ⇒ Object
72 73 74 75 |
# File 'lib/kumi/schema.rb', line 72 def from(input_data) ensure_compiled! CompiledSchemaWrapper.new(__kumi_compiled_module__, input_data) end |
#runner ⇒ Object
66 67 68 69 70 |
# File 'lib/kumi/schema.rb', line 66 def runner ensure_compiled! CompiledSchemaWrapper.new(__kumi_compiled_module__, {}) end |
#schema(&block) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/kumi/schema.rb', line 52 def schema(&block) @__kumi_syntax_tree__ = Kumi::Core::RubyParser::Dsl.build_syntax_tree(&block) # Store the location where the schema was defined. This is essential for caching # and providing good error messages. @kumi_source_location = block.source_location.first ensure_compiled! end |
#schema_metadata ⇒ Object
61 62 63 64 |
# File 'lib/kumi/schema.rb', line 61 def ensure_compiled! SchemaMetadata.new(@__kumi_analyzer_result__.state, @__kumi_syntax_tree__) end |
#write_source(file_path, platform: :ruby) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/kumi/schema.rb', line 77 def write_source(file_path, platform: :ruby) raise "No schema defined" unless @__kumi_syntax_tree__ raise ArgumentError, "platform must be :ruby or :javascript" unless %i[ruby javascript].include?(platform) result = Kumi::Analyzer.analyze!(@__kumi_syntax_tree__) code = case platform when :ruby result.state[:ruby_codegen_files]&.fetch("codegen.rb", nil) when :javascript result.state[:javascript_codegen_files]&.fetch("codegen.mjs", nil) end raise "Compiler did not produce #{platform}_codegen_files" unless code FileUtils.mkdir_p(File.dirname(file_path)) File.write(file_path, code) file_path end |