Class: Moxml::StructPlan
- Inherits:
-
Object
- Object
- Moxml::StructPlan
- Defined in:
- lib/moxml/struct_plan.rb
Overview
Struct-compiled materialization — the fully compiled grammar: declare the document shape once as STRUCTS; adapters with a C executor (leptris 1.9.197.1+) mint the typed objects directly with zero Ruby frames per element; everything else falls back to an equivalent Moxml::Plan over the row stream (spec-pinned equal).
plan = Moxml::StructPlan.new do
element "record", Record, attrs: { "id" => :id, "kind" => :kind },
children: :fields
element "field", Field, attrs: { "name" => :name, "unit" => :unit },
text: :value
end
records = plan.parse(xml, ctx) # Array of Record
Slot symbols must be the Struct's member names. Semantics equal Moxml::Plan: unmatched elements are barriers — their matched descendants surface at top level.
Defined Under Namespace
Classes: Element
Instance Method Summary collapse
-
#compile ⇒ Object
The compiled spec the C executor consumes: => [klass, attrs, text slot, children slot].
-
#element(name, klass, attrs: {}, text: nil, children: nil) ⇒ Object
Registers a struct element.
-
#initialize(&block) ⇒ StructPlan
constructor
A new instance of StructPlan.
- #materialize(node) ⇒ Object
- #parse(xml, context) ⇒ Object
Constructor Details
#initialize(&block) ⇒ StructPlan
Returns a new instance of StructPlan.
25 26 27 28 |
# File 'lib/moxml/struct_plan.rb', line 25 def initialize(&block) @elements = {} instance_eval(&block) if block end |
Instance Method Details
#compile ⇒ Object
The compiled spec the C executor consumes: => [klass, attrs, text slot, children slot].
41 42 43 44 45 |
# File 'lib/moxml/struct_plan.rb', line 41 def compile @elements.each_with_object({}) do |(name, el), spec| spec[name] = [el.klass, el.attrs, el.text, el.children] end end |
#element(name, klass, attrs: {}, text: nil, children: nil) ⇒ Object
Registers a struct element. attrs maps attribute names to
member slots; text and children name the member slots
receiving the first text child and the matched children array.
Returns self so registrations chain.
34 35 36 37 |
# File 'lib/moxml/struct_plan.rb', line 34 def element(name, klass, attrs: {}, text: nil, children: nil) @elements[name] = Element.new(klass, attrs, text, children) self end |
#materialize(node) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/moxml/struct_plan.rb', line 51 def materialize(node) adapter = node.context.config.adapter roots = adapter.plan_structs(node.native, compile) return roots if roots plan.materialize(node) end |
#parse(xml, context) ⇒ Object
47 48 49 |
# File 'lib/moxml/struct_plan.rb', line 47 def parse(xml, context) materialize(context.parse(xml)) end |