class Prolog::Services::ReplaceContent

Replaces content within an HTML string based on endpoints and content. FIXME: Reek thinks this has :reek:TooManyInstanceVariables; cleanup soonn.

Replaces content within an HTML string based on endpoints and content.

Replaces content within an HTML string based on endpoints and content.

Replaces content within an HTML string based on endpoints and content.

Replaces content within an HTML string based on endpoints and content.

Replaces content within an HTML string based on endpoints and content.

Replaces content within an HTML string based on endpoints and content.

Replaces content within an HTML string based on endpoints and content.

Constants

VERSION

Attributes

content[R]
endpoints[R]
errors[R]
markers[R]
replacement[R]

Public Class Methods

new(content: '', endpoints: (-1..-1), replacement: '') click to toggle source
# File lib/prolog/services/replace_content.rb, line 15
def initialize(content: '', endpoints: (-1..-1), replacement: '')
  @content = content
  @endpoints = endpoints
  @replacement = replacement
  @content_after_conversion = nil
  @errors = {}
  self
end
set_content(obj, content) click to toggle source
# File lib/prolog/services/replace_content.rb, line 48
def self.set_content(obj, content)
  new endpoints: obj.endpoints, replacement: obj.replacement,
      content: content
end
set_endpoints(obj, endpoints) click to toggle source
# File lib/prolog/services/replace_content.rb, line 53
def self.set_endpoints(obj, endpoints)
  new content: obj.content, replacement: obj.replacement,
      endpoints: endpoints
end
set_replacement(obj, replacement) click to toggle source
# File lib/prolog/services/replace_content.rb, line 58
def self.set_replacement(obj, replacement)
  new content: obj.content, endpoints: obj.endpoints,
      replacement: replacement
end

Public Instance Methods

convert() click to toggle source
# File lib/prolog/services/replace_content.rb, line 24
def convert
  validate
  return false unless valid?

  set_converted_content
  valid?
end
converted_content() click to toggle source

rubocop:enable

# File lib/prolog/services/replace_content.rb, line 33
def converted_content
  errors[:conversion] = ['not called'] unless @content_after_conversion
  @content_after_conversion || :oops
end
markers=(*params) click to toggle source

Setting markers also invalidates the conversion.

# File lib/prolog/services/replace_content.rb, line 39
def markers=(*params)
  @content_after_conversion = nil
  @markers = Array(params).flatten
end
valid?() click to toggle source
# File lib/prolog/services/replace_content.rb, line 44
def valid?
  errors.empty?
end

Private Instance Methods

_invalidate_endpoints() click to toggle source
# File lib/prolog/services/replace_content.rb, line 107
def _invalidate_endpoints
  errors[:endpoints] = %w[invalid]
  false
end
build_converted_content() click to toggle source
# File lib/prolog/services/replace_content.rb, line 65
def build_converted_content
  replace_inner_content_in splitter.source
end
parse_with_comments() click to toggle source
# File lib/prolog/services/replace_content.rb, line 79
def parse_with_comments
  comment = '<!-- -->'
  twiddled = splitter(comment).source
  ret = Ox.parse twiddled # raises on most errors
  ret
end
replace_inner_content_in(markup) click to toggle source
# File lib/prolog/services/replace_content.rb, line 69
def replace_inner_content_in(markup)
  markup.sub(splitter.inner, replacement)
end
set_converted_content() click to toggle source
# File lib/prolog/services/replace_content.rb, line 73
def set_converted_content
  html = build_converted_content
  @content_after_conversion = html if validate_markup(html, :replacement)
  @content_after_conversion
end
splitter(marker = Splitter::Symmetric::DEFAULT_MARKER) click to toggle source
# File lib/prolog/services/replace_content.rb, line 86
def splitter(marker = Splitter::Symmetric::DEFAULT_MARKER)
  ret = Splitter::Factory.call(self, marker)
  ret
end
validate() click to toggle source
# File lib/prolog/services/replace_content.rb, line 91
def validate
  validate_content && validate_endpoints
end
validate_content() click to toggle source
# File lib/prolog/services/replace_content.rb, line 95
def validate_content
  validate_markup @content, :content
end
validate_endpoints() click to toggle source
# File lib/prolog/services/replace_content.rb, line 112
def validate_endpoints
  parse_with_comments
  true
rescue Ox::ParseError
  _invalidate_endpoints
rescue RangeError
  _invalidate_endpoints
end
validate_markup(markup, error_key) click to toggle source
# File lib/prolog/services/replace_content.rb, line 99
def validate_markup(markup, error_key)
  Ox.parse markup
  true
rescue Ox::ParseError
  errors[error_key] = ['invalid']
  false
end