Module: Kumi::Schema

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

Instance Method Summary collapse

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_treeObject



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

#runnerObject



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_metadataObject



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

Raises:

  • (ArgumentError)


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