Module: Oselvar::Var::Internal

Defined in:
lib/oselvar/var/internal.rb

Overview

The module-scope step-registration accumulator behind the block DSL steps(...) do stimulus(...); sensor(...) end. Mirrors @oselvar/var's internal.ts. A step file, when loaded, calls steps() once; the Builder its block registers into these accumulators. The runner/harness then reads them via build_registry / context_factory.

Defined Under Namespace

Classes: Builder

Class Method Summary collapse

Class Method Details

.add_custom_type(name, regexp, parse, format) ⇒ Object

Accumulate one custom parameter type.



41
42
43
44
# File 'lib/oselvar/var/internal.rb', line 41

def add_custom_type(name, regexp, parse, format)
  @custom_types << { name: name, regexp: regexp, parse: parse, format: format }
  nil
end

.add_step(expression, handler, kind) ⇒ Object

Accumulate one step. The handler's source_location anchors it to the line the block is written on.



31
32
33
34
35
36
37
38
# File 'lib/oselvar/var/internal.rb', line 31

def add_step(expression, handler, kind)
  file, line = handler.source_location
  @steps << {
    expression: expression, source_file: file, source_line: line,
    handler: handler, kind: kind
  }
  nil
end

.build_registryObject

Build a Core::Registry: custom parameter types first (so expressions can reference them), then steps in registration order.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/oselvar/var/internal.rb', line 57

def build_registry
  registry = Core::Registries.create_registry
  @custom_types.each do |type|
    registry = Core::Registries.define_parameter_type(
      registry, name: type[:name], regexp: type[:regexp], parse: type[:parse], format: type[:format]
    )
  end
  @steps.each do |step|
    registry = Core::Registries.add_step(
      registry,
      expression: step[:expression],
      expression_source_file: step[:source_file],
      expression_source_line: step[:source_line],
      handler: step[:handler],
      kind: step[:kind]
    )
  end
  registry
end

.context_factoryObject

(step_file) -> state: invoke the file's factory, or {} if none.



47
48
49
50
51
52
53
# File 'lib/oselvar/var/internal.rb', line 47

def context_factory
  factories = @context_factories_by_file.dup
  lambda do |step_file|
    factory = factories[step_file]
    factory ? factory.call : {}
  end
end

.custom_parameter_typesObject

Conformance-harness accessor: custom parameter types projected to the "name","regexp" wire shape. regexp is the bare source (Regexp#source or the string as authored) — the cross-port convention.



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/oselvar/var/internal.rb', line 87

def custom_parameter_types
  @custom_types.map do |type|
    regexp = type[:regexp]
    regexp = regexp.source if regexp.is_a?(Regexp)
    unless regexp.is_a?(String)
      raise "parameter type #{type[:name].inspect}: regexp arrays are not supported " \
            'by the conformance projection yet'
    end
    { 'name' => type[:name], 'regexp' => regexp }
  end
end

.register(factory, source_file) ⇒ Object

Register a file's state factory and return a Builder whose stimulus/sensor/param methods accumulate that file's steps. factory is a callable (or nil for empty state); source_file keys the per-file context factory. Raises if called twice for one file.



22
23
24
25
26
27
# File 'lib/oselvar/var/internal.rb', line 22

def register(factory, source_file)
  raise "steps() called more than once in #{source_file}" if @context_factories_by_file.key?(source_file)

  @context_factories_by_file[source_file] = factory || -> { {} }
  Builder.new
end

.reset_builderObject

Clear all accumulated state (between isolated runs / harness bundles).



78
79
80
81
82
# File 'lib/oselvar/var/internal.rb', line 78

def reset_builder
  @steps = []
  @context_factories_by_file = {}
  @custom_types = []
end