Class: Journeybook::ComponentTagUpdater
- Inherits:
-
Object
- Object
- Journeybook::ComponentTagUpdater
- Defined in:
- app/services/journeybook/component_tag_updater.rb
Defined Under Namespace
Classes: ComponentTag
Instance Method Summary collapse
- #append(markdown, name, props = {}) ⇒ Object
- #delete(markdown, index) ⇒ Object
-
#initialize(parser: ComponentParser.new) ⇒ ComponentTagUpdater
constructor
A new instance of ComponentTagUpdater.
- #move(markdown, index, direction) ⇒ Object
- #reorder(markdown, indexes) ⇒ Object
- #toggle_visibility(markdown, index) ⇒ Object
- #update(markdown, index, props) ⇒ Object
Constructor Details
#initialize(parser: ComponentParser.new) ⇒ ComponentTagUpdater
Returns a new instance of ComponentTagUpdater.
5 6 7 |
# File 'app/services/journeybook/component_tag_updater.rb', line 5 def initialize(parser: ComponentParser.new) @parser = parser end |
Instance Method Details
#append(markdown, name, props = {}) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'app/services/journeybook/component_tag_updater.rb', line 54 def append(markdown, name, props = {}) source = serialize(name, normalized_props(props)) body = markdown.to_s.rstrip return source if body.blank? "#{body}\n\n#{source}" end |
#delete(markdown, index) ⇒ Object
35 36 37 38 |
# File 'app/services/journeybook/component_tag_updater.rb', line 35 def delete(markdown, index) tag = fetch_tag((markdown), index) replace_sources(markdown, tag => "").gsub(/\n{3,}/, "\n\n") end |
#move(markdown, index, direction) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/services/journeybook/component_tag_updater.rb', line 15 def move(markdown, index, direction) = (markdown) tag = fetch_tag(, index) target_index = direction.to_s == "up" ? index - 1 : index + 1 target = fetch_tag(, target_index) replace_sources(markdown, { tag => target.source, target => tag.source }) end |
#reorder(markdown, indexes) ⇒ Object
27 28 29 30 31 32 33 |
# File 'app/services/journeybook/component_tag_updater.rb', line 27 def reorder(markdown, indexes) = (markdown) indexes = Array(indexes).map(&:to_i) raise ArgumentError unless indexes.sort == (0....length).to_a replace_sources(markdown, .zip(indexes.map { |index| .fetch(index).source }).to_h) end |
#toggle_visibility(markdown, index) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/services/journeybook/component_tag_updater.rb', line 40 def toggle_visibility(markdown, index) components = @parser.parse(markdown) component = components.fetch(index) props = component.props.dup if component.hidden? props.delete(ComponentParser::HIDDEN_PROP) else props[ComponentParser::HIDDEN_PROP] = "true" end replace_component(markdown, index, component.name, props) end |
#update(markdown, index, props) ⇒ Object
9 10 11 12 13 |
# File 'app/services/journeybook/component_tag_updater.rb', line 9 def update(markdown, index, props) components = @parser.parse(markdown) component = components.fetch(index) replace_component(markdown, index, component.name, component.props.merge(normalized_props(props))) end |