Module: Kumi::Schema

Overview

This module is the main entry point for users of the Kumi DSL. When a user module extends 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.



44
45
46
# File 'lib/kumi/schema.rb', line 44

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.



44
45
46
# File 'lib/kumi/schema.rb', line 44

def __kumi_syntax_tree__
  @__kumi_syntax_tree__
end

Instance Method Details

#build_syntax_treeObject



47
48
49
# File 'lib/kumi/schema.rb', line 47

def build_syntax_tree(&)
  @__kumi_syntax_tree__ = Kumi::Core::RubyParser::Dsl.build_syntax_tree(&)
end

#from(input_data) ⇒ Object



71
72
73
74
# File 'lib/kumi/schema.rb', line 71

def from(input_data)
  ensure_compiled!
  CompiledSchemaWrapper.new(__kumi_compiled_module__, input_data)
end

#runnerObject



65
66
67
68
69
# File 'lib/kumi/schema.rb', line 65

def runner
  ensure_compiled!

  CompiledSchemaWrapper.new(__kumi_compiled_module__, {})
end

#schema(&block) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/kumi/schema.rb', line 51

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



60
61
62
63
# File 'lib/kumi/schema.rb', line 60

def 
  ensure_compiled!
  .new(@__kumi_analyzer_result__.state, @__kumi_syntax_tree__)
end

#write_source(file_path, platform: :ruby) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kumi/schema.rb', line 76

def write_source(file_path, platform: :ruby)
  raise Kumi::Core::Errors::ConfigurationError, "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 Kumi::Core::Errors::CompilerBug, "compiler did not produce #{platform}_codegen_files" unless code

  FileUtils.mkdir_p(File.dirname(file_path))
  File.write(file_path, code)
  file_path
end