Sha256: ad8f0e87c5b5de540016477fc5a84c5e59f34d305f5f84da03398e044e83d520

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

# Non-Self-Closing tag. Can have content, but doesn't have to.
class DoubleTag < SingleTag
  attr_accessor :oneline
  attr_reader :content

  # Content represents everything between the opening and closing tags.

  def initialize(element, attributes: nil, content: nil, oneline: false)
    @oneline = oneline
    self.content = content
    super(element, attributes: attributes)
  end

  def content=(new)
    reset_content
    add_content(new)
  end

  def reset_content
    @content = []
  end

  def add_content(addition)
    @content << addition if addition
    @content.flatten!
    self
  end
  alias << add_content

  def to_a
    lines = content.map { |c| build_content_line c }
    lines = lines.flatten.map { |l| l.prepend oneline ? '' : indent }
    lines.unshift(opening_tag).push(closing_tag)
  end

  def to_s
    to_a.join(oneline ? '' : "\n") + "\n"
  end

  private

  def build_content_line(element)
    # Since DoubleTag inherits from SingleTag, it will slurp up those too.
    element.is_a?(SingleTag) ? element.to_a : element.to_s.dup
  end

  def indent
    "\ \ "
  end

  def closing_tag
    "</#{element}>"
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
objective_elements-1.1.2 lib/objective_elements/double_tag.rb
objective_elements-1.1.1 lib/objective_elements/double_tag.rb
objective_elements-1.1.0 lib/objective_elements/double_tag.rb