Class: HtmlSurgeon::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/html_surgeon/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html_string, audit: false, **extra_options) ⇒ Service

Returns a new instance of Service.



5
6
7
8
9
# File 'lib/html_surgeon/service.rb', line 5

def initialize(html_string, audit: false, **extra_options)
  @given_html = html_string.to_s
  @audit      = audit
  @options    = extra_options.merge audit: audit
end

Instance Attribute Details

#given_htmlObject (readonly)

Returns the value of attribute given_html.



3
4
5
# File 'lib/html_surgeon/service.rb', line 3

def given_html
  @given_html
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/html_surgeon/service.rb', line 3

def options
  @options
end

Instance Method Details

#audit?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/html_surgeon/service.rb', line 15

def audit?
  !!@audit
end

#clear_auditObject



40
41
42
43
44
45
# File 'lib/html_surgeon/service.rb', line 40

def clear_audit
  doc.css("[#{DATA_CHANGE_AUDIT_ATTRIBUTE}]").reduce(0) do |sum, node|
    cleaner = NodeServices::AuditCleaner.new node: node
    sum + cleaner.call
  end
end

#css(css_selector) ⇒ Object



19
20
21
22
# File 'lib/html_surgeon/service.rb', line 19

def css(css_selector)
  node_set = doc.css(css_selector)
  ChangeSet.create(node_set, self)
end

#htmlObject



11
12
13
# File 'lib/html_surgeon/service.rb', line 11

def html
  @html ||= doc.to_html
end

#rollback(change_set: nil, changed_at: nil, changed_from: nil) ⇒ Object

returns the number of changes performed



30
31
32
33
34
35
36
37
38
# File 'lib/html_surgeon/service.rb', line 30

def rollback(change_set: nil, changed_at: nil, changed_from: nil)
  doc.css("[#{DATA_CHANGE_AUDIT_ATTRIBUTE}]").reduce(0) do |sum, node|
    reverser = NodeServices::Reverser.new node:         node,
                                change_set:   change_set,
                                changed_at:   changed_at,
                                changed_from: changed_from
    sum + reverser.call
  end
end

#xpath(xpath_selector) ⇒ Object



24
25
26
27
# File 'lib/html_surgeon/service.rb', line 24

def xpath(xpath_selector)
  node_set = doc.xpath(xpath_selector)
  ChangeSet.create(node_set, self)
end