Class: Prosereflect::Transform::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/prosereflect/transform/mapping.rb

Overview

Tracks position changes through a series of steps. Maps positions forward through the transformation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maps: []) ⇒ Mapping

Returns a new instance of Mapping.



13
14
15
16
17
# File 'lib/prosereflect/transform/mapping.rb', line 13

def initialize(maps: [])
  @maps = maps.dup
  @from = 0
  @to = maps.length
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



11
12
13
# File 'lib/prosereflect/transform/mapping.rb', line 11

def from
  @from
end

#mapsObject (readonly)

Returns the value of attribute maps.



10
11
12
# File 'lib/prosereflect/transform/mapping.rb', line 10

def maps
  @maps
end

#toObject

Returns the value of attribute to.



11
12
13
# File 'lib/prosereflect/transform/mapping.rb', line 11

def to
  @to
end

Class Method Details

.from_step_map(step_map) ⇒ Object

Create from a single step map



69
70
71
# File 'lib/prosereflect/transform/mapping.rb', line 69

def self.from_step_map(step_map)
  new(maps: [step_map])
end

Instance Method Details

#add_map(step_map, index = nil) ⇒ Object

Add a step map to this mapping



20
21
22
23
24
25
26
27
# File 'lib/prosereflect/transform/mapping.rb', line 20

def add_map(step_map, index = nil)
  if index
    @maps.insert(index, step_map)
  else
    @maps << step_map
  end
  @to = @maps.length
end

#inspectObject



77
78
79
# File 'lib/prosereflect/transform/mapping.rb', line 77

def inspect
  to_s
end

#map(pos, on_del: nil) ⇒ Object

Map a position through all steps in this mapping



30
31
32
33
34
35
# File 'lib/prosereflect/transform/mapping.rb', line 30

def map(pos, on_del: nil) # rubocop:disable Lint:UnusedMethodArgument
  @maps.each do |step_map|
    pos = step_map.map(pos)
  end
  pos
end

#map_deletes(pos) ⇒ Object

Check if a position was deleted



59
60
61
# File 'lib/prosereflect/transform/mapping.rb', line 59

def map_deletes(pos)
  @maps.any? { |step_map| step_map.deleted?(pos) }
end

#map_result(pos, on_del: nil) ⇒ Object

Map a position with deletion tracking



38
39
40
41
42
43
44
45
46
# File 'lib/prosereflect/transform/mapping.rb', line 38

def map_result(pos, on_del: nil)
  deleted = false
  @maps.each do |step_map|
    result = step_map.map_result(pos, on_del: on_del)
    deleted ||= result.deleted
    pos = result.pos
  end
  { pos: pos, deleted: deleted }
end

#map_reverse(pos) ⇒ Object

Map a position backwards through the mapping



49
50
51
52
53
54
55
56
# File 'lib/prosereflect/transform/mapping.rb', line 49

def map_reverse(pos)
  result = pos
  (0...@maps.length).each do |i|
    step_map = @maps[@maps.length - 1 - i]
    result = step_map.map_reverse(result)
  end
  result
end

#to_aObject

Get the mapping as an array of step maps



64
65
66
# File 'lib/prosereflect/transform/mapping.rb', line 64

def to_a
  @maps.dup
end

#to_sObject



73
74
75
# File 'lib/prosereflect/transform/mapping.rb', line 73

def to_s
  "<Mapping maps=#{@maps.length}>"
end