class MarkdownToRspec::ToRspec::Item::Heading

Attributes

children[RW]
depth[R]
level[R]
parent[R]
text[R]

Public Class Methods

new(level:, text:, parent: nil, children: []) click to toggle source
# File lib/markdown_to_rspec/to_rspec/item/heading.rb, line 7
def initialize(level:, text:, parent: nil, children: [])
  @level = level
  @text = text
  @parent = parent
  @children = children
  # If there is a parent, nest it and add it to the parent's child elements.
  @depth = parent ? parent.depth + 1 : 0
end

Public Instance Methods

convert() click to toggle source
# File lib/markdown_to_rspec/to_rspec/item/heading.rb, line 19
def convert
  template(level_to_syntax, text)
    .split("\n")
    .map { |line| indent(line) }
    .insert(1, convert_children) # Insert the result of a child element between do...end.
    .join("\n")
end

Private Instance Methods

convert_children() click to toggle source
# File lib/markdown_to_rspec/to_rspec/item/heading.rb, line 29
def convert_children
  children.map(&:convert).join("\n")
end
level_to_syntax() click to toggle source
# File lib/markdown_to_rspec/to_rspec/item/heading.rb, line 40
def level_to_syntax
  case level
  when 1 then 'RSpec.describe'
  when 2 then 'describe'
  else 'context'
  end
end
template(syntax, text) click to toggle source
# File lib/markdown_to_rspec/to_rspec/item/heading.rb, line 33
        def template(syntax, text)
          <<~TEXT
            #{syntax} '#{text.gsub(/"|'/, '')}' do
            end
          TEXT
        end