Class: Lorax::InsertDelta

Inherits:
Delta
  • Object
show all
Defined in:
lib/lorax/delta/insert_delta.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Delta

#inspect

Constructor Details

#initialize(node, xpath, position) ⇒ InsertDelta

Returns a new instance of InsertDelta.


5
6
7
8
9
# File 'lib/lorax/delta/insert_delta.rb', line 5

def initialize(node, xpath, position)
  @node     = node
  @xpath    = xpath
  @position = position
end

Instance Attribute Details

#nodeObject

Returns the value of attribute node


3
4
5
# File 'lib/lorax/delta/insert_delta.rb', line 3

def node
  @node
end

#positionObject

Returns the value of attribute position


3
4
5
# File 'lib/lorax/delta/insert_delta.rb', line 3

def position
  @position
end

#xpathObject

Returns the value of attribute xpath


3
4
5
# File 'lib/lorax/delta/insert_delta.rb', line 3

def xpath
  @xpath
end

Instance Method Details

#apply!(document) ⇒ Object

Raises:


11
12
13
14
15
16
# File 'lib/lorax/delta/insert_delta.rb', line 11

def apply!(document)
  # TODO: patch nokogiri to make inserting node copies efficient
  parent = document.at_xpath(xpath)
  raise NodeNotFoundError, xpath unless parent
  insert_node(node.dup, parent, position)
end

#descriptorObject


18
19
20
# File 'lib/lorax/delta/insert_delta.rb', line 18

def descriptor
  [:insert, {:xpath => xpath, :position => position, :content => node.to_s}]
end

#to_sObject


22
23
24
25
26
27
28
29
30
# File 'lib/lorax/delta/insert_delta.rb', line 22

def to_s
  response = []
  response << "---"
  response << "+++ #{node.path}"
  response << context_before(node)
  response << node.to_html.gsub(/^/,'+ ').strip
  response << context_after(node)
  response.join("\n")
end