Class: GacoCms::ShortcodeParser

Inherits:
ApplicationService show all
Defined in:
app/services/gaco_cms/shortcode_parser.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationService

call, #delay

Constructor Details

#initialize(content, record, ignore: []) ⇒ ShortcodeParser

Returns a new instance of ShortcodeParser.



7
8
9
10
11
12
# File 'app/services/gaco_cms/shortcode_parser.rb', line 7

def initialize(content, record, ignore: [])
  @content = content
  @record = record
  @ignored_shortcodes = Array(ignore)
  super
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



5
6
7
# File 'app/services/gaco_cms/shortcode_parser.rb', line 5

def content
  @content
end

#ignored_shortcodesObject (readonly)

Returns the value of attribute ignored_shortcodes.



5
6
7
# File 'app/services/gaco_cms/shortcode_parser.rb', line 5

def ignored_shortcodes
  @ignored_shortcodes
end

#recordObject (readonly)

Returns the value of attribute record.



5
6
7
# File 'app/services/gaco_cms/shortcode_parser.rb', line 5

def record
  @record
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/gaco_cms/shortcode_parser.rb', line 14

def call
  counter = 0
  loop do
    detected = @content.scan(/#{shortcodes_regex}/).to_a
    break if detected.empty?
    break log("Too many levels detected, skipping after #{counter} levels", mode: :error) if counter > 5

    detected.each { |item| @content = replace_shortcode(item) }
    counter += 1
  end
  @content
end