Class: GovukTechDocs::TableOfContents::Heading

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_tech_docs/table_of_contents/heading.rb

Instance Method Summary collapse

Constructor Details

#initialize(element_name:, text:, attributes:, page_url: "") ⇒ Heading

Returns a new instance of Heading.



4
5
6
7
8
9
# File 'lib/govuk_tech_docs/table_of_contents/heading.rb', line 4

def initialize(element_name:, text:, attributes:, page_url: "")
  @element_name = element_name
  @text = text
  @attributes = attributes
  @page_url = page_url
end

Instance Method Details

#==(other) ⇒ Object



27
28
29
30
31
# File 'lib/govuk_tech_docs/table_of_contents/heading.rb', line 27

def ==(other)
  @element_name == other.instance_variable_get("@element_name") &&
    @text == other.instance_variable_get("@text") &&
    @attributes == other.instance_variable_get("@attributes")
end

#hrefObject



15
16
17
18
19
20
21
# File 'lib/govuk_tech_docs/table_of_contents/heading.rb', line 15

def href
  if @page_url != "" && size == 1
    @page_url
  else
    "#{@page_url}##{@attributes['id']}"
  end
end

#sizeObject



11
12
13
# File 'lib/govuk_tech_docs/table_of_contents/heading.rb', line 11

def size
  @element_name.scan(/h(\d)/) && ::Regexp.last_match(1) && Integer(::Regexp.last_match(1))
end

#titleObject



23
24
25
# File 'lib/govuk_tech_docs/table_of_contents/heading.rb', line 23

def title
  @text
end