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, use_helper) ⇒ Substitution

Returns a new instance of Substitution.



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

def initialize(name, xpath, type, value, key, use_helper)
  @name, @xpath, @type, @value, @key, @use_helper = name, xpath, type, value, key, use_helper
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

#value(node) ⇒ Object (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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/flowcation/substitution.rb', line 14

def apply(doc)
  # todo Refactor to SubstitutionType class with ContentSubstitution, AttributeSubstitution...
  
  # todo Better type names. More Like Targets in Stimulus? each->attribute
  #   Even Helpers could be included in type: each-replace->LayoutHelper#image_tag
  
  element = doc.at_xpath(@xpath)
  raise SubstitutionNotFoundException.build(xpath: @xpath, name: @name) unless element
  #self.class.build_substitution_type(@type).substitute(doc)
  case @type
  when 'content'
    doc.at_xpath(@xpath).content = value(doc.at_xpath(@xpath))
  when 'content_collection'
    doc.xpath(@xpath).each do |node|
      node.content = value(node)
    end
  when 'attribute'
    doc.at_xpath(@xpath)[@key] = value(doc.at_xpath(@xpath))
  when 'attribute_collection'
    doc.xpath(@xpath).each do |node|
      node[@key] = value(node)
    end
  when 'replace'
    doc.at_xpath(@xpath).replace Nokogiri::XML::Text.new(value(doc.at_xpath(@xpath)), doc.document)
  when 'replace_each'
    doc.xpath(@xpath).each do |node|
      node.replace Nokogiri::XML::Text.new(value(node), doc.document)
    end
  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
  when 'append'
    puts "APPEND #{doc.at_xpath(@xpath).class}"
    doc.at_xpath(@xpath).after Nokogiri::XML::Text.new(value(doc.at_xpath(@xpath)), doc.document)
  end
  doc
end