Class: DoubleTag

Inherits:
SingleTag show all
Defined in:
lib/objective_elements/double_tag.rb

Overview

Non-Self-Closing tag. Can have content, but doesn’t have to.

Instance Attribute Summary collapse

Attributes inherited from SingleTag

#attributes, #element

Instance Method Summary collapse

Methods inherited from SingleTag

#add_attributes, #add_parent, #delete_attributes, #opening_tag, #render_attributes, #reset_attributes, #rewrite_attribute

Constructor Details

#initialize(element, attributes: nil, content: [], oneline: false) ⇒ DoubleTag

content is an array of anything. Entries will be inserted sequentially in between the opening and closing tags.



7
8
9
10
11
# File 'lib/objective_elements/double_tag.rb', line 7

def initialize(element, attributes: nil, content: [], oneline: false)
  @oneline = oneline
  reset_content(content)
  super(element, attributes: attributes)
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/objective_elements/double_tag.rb', line 3

def content
  @content
end

#onelineObject

Returns the value of attribute oneline.



3
4
5
# File 'lib/objective_elements/double_tag.rb', line 3

def oneline
  @oneline
end

Instance Method Details

#add_content(addition) ⇒ Object



24
25
26
27
28
# File 'lib/objective_elements/double_tag.rb', line 24

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

#closing_tagObject



13
14
15
# File 'lib/objective_elements/double_tag.rb', line 13

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

#indentObject



30
31
32
# File 'lib/objective_elements/double_tag.rb', line 30

def indent
  "\ \ "
end

#reset_content(new = nil) ⇒ Object

Deletes all content, replaces with parameter (if supplied)



18
19
20
21
22
# File 'lib/objective_elements/double_tag.rb', line 18

def reset_content(new = nil)
  @content = []
  add_content(new) if new
  self
end

#to_aObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/objective_elements/double_tag.rb', line 34

def to_a
  lines = content.map do |c|
    if c.is_a? SingleTag
      c.to_a
    else
      c.to_s.dup
    end
  end
  lines = lines.flatten.map { |l| l.prepend oneline ? '' : indent }
  lines.unshift(opening_tag).push(closing_tag)
end

#to_sObject



46
47
48
# File 'lib/objective_elements/double_tag.rb', line 46

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