Class: Flowcation::Substitution

Inherits:
Object
  • Object
show all
Defined in:
lib/flowcation/substitution.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, xpath, type, value, key) ⇒ Substitution

Returns a new instance of Substitution.



4
5
6
# File 'lib/flowcation/substitution.rb', line 4

def initialize(name, xpath, type, value, key)
  @name, @xpath, @type, @value, @key = name, xpath, type, value, key
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/flowcation/substitution.rb', line 3

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/flowcation/substitution.rb', line 3

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



3
4
5
# File 'lib/flowcation/substitution.rb', line 3

def value
  @value
end

#xpathObject (readonly)

Returns the value of attribute xpath.



3
4
5
# File 'lib/flowcation/substitution.rb', line 3

def xpath
  @xpath
end

Instance Method Details

#apply(doc) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/flowcation/substitution.rb', line 7

def apply(doc)
  element = doc.at_xpath(@xpath)
  raise SubstitutionNotFoundException.build(xpath: @xpath, name: @name) unless element
  case @type
  when 'content'
    doc.at_xpath(@xpath).content = @value
  when 'attribute'
    doc.at_xpath(@xpath).attributes[@key].value = @value
  when 'replace'
    doc.at_xpath(@xpath).replace Nokogiri::XML::Text.new(@value, doc.document)
  when 'append'
    puts "APPEND #{doc.at_xpath(@xpath).class}"
    doc.at_xpath(@xpath).after Nokogiri::XML::Text.new(@value, doc.document)
  when 'replace_collection'
    doc.xpath(@xpath).first.replace Nokogiri::XML::Text.new(@value, doc.document)
    doc.xpath(@xpath).each do |node|
      node.remove if node.is_a? Nokogiri::XML::Element
    end
  end
  doc
end