Class: Prosereflect::Transform::AttrStep

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

Overview

Set or remove attributes on a node at a position

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Step

#merge, #to

Constructor Details

#initialize(pos, attrs) ⇒ AttrStep

Returns a new instance of AttrStep.



12
13
14
15
16
# File 'lib/prosereflect/transform/attr_step.rb', line 12

def initialize(pos, attrs)
  super()
  @pos = pos
  @attrs = attrs
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



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

def attrs
  @attrs
end

#posObject (readonly)

Returns the value of attribute pos.



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

def pos
  @pos
end

Class Method Details

.from_json(_schema, json) ⇒ Object



50
51
52
# File 'lib/prosereflect/transform/attr_step.rb', line 50

def self.from_json(_schema, json)
  new(json["pos"], json["attrs"])
end

Instance Method Details

#apply(doc) ⇒ Object



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

def apply(doc)
  return Result.fail("Invalid position") if @pos.negative? || @pos > doc.node_size

  begin
    new_doc = set_node_attrs(doc)
    Result.ok(new_doc)
  rescue StandardError => e
    Result.fail(e.message)
  end
end

#get_mapObject



29
30
31
# File 'lib/prosereflect/transform/attr_step.rb', line 29

def get_map
  StepMap.new
end

#invert(doc) ⇒ Object



33
34
35
36
37
# File 'lib/prosereflect/transform/attr_step.rb', line 33

def invert(doc)
  # Find what attrs were changed and revert them
  old_attrs = get_old_attrs(doc)
  AttrStep.new(@pos, old_attrs)
end

#step_typeObject



39
40
41
# File 'lib/prosereflect/transform/attr_step.rb', line 39

def step_type
  "setAttr"
end

#to_json(*_args) ⇒ Object



43
44
45
46
47
48
# File 'lib/prosereflect/transform/attr_step.rb', line 43

def to_json(*_args)
  json = super
  json["pos"] = @pos
  json["attrs"] = @attrs
  json
end