Module: Punctuated::Parsed::Composition

Included in:
Punctuated::Parsed
Defined in:
lib/punctuated/parsed/composition.rb

Overview

Recombines parsed state and provides the single commit path for all component changes.

Instance Method Summary collapse

Instance Method Details

#change_component(side, kind, attributes) ⇒ Object

Raw and placement setters enter here so a public mutation commits one coordinated pair.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/punctuated/parsed/composition.rb', line 23

def change_component(side, kind, attributes)
  change = {}
  if attributes.key?(:raw)
    change[:raw] = attributes[:raw]
  end
  if attributes.key?(:placement)
    change[:placement] = attributes[:placement]
    change[:placement_specified] = true
  end

  apply_component_changes(side => { kind => change })
  component(side, kind)
end

#component_index(target) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/punctuated/parsed/composition.rb', line 12

def component_index(target)
  offset = 0
  result_parts.each do |part|
    return offset if part.equal?(target)

    offset += part.is_a?(Component) ? part.raw.length : part.length
  end
  -1
end

#resultObject

Rebuilds from current state on every call, so returned strings can be mutated safely.



8
9
10
# File 'lib/punctuated/parsed/composition.rb', line 8

def result
  result_parts.map { |part| part.is_a?(Component) ? part.raw : part }.join
end