Class: ArticleJSON::Elements::Text

Inherits:
Base
  • Object
show all
Defined in:
lib/article_json/elements/text.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

parse_hash_list

Constructor Details

#initialize(content:, bold: false, italic: false, href: nil) ⇒ Text

Returns a new instance of Text.

Parameters:

  • content (String)
  • bold (Boolean) (defaults to: false)
  • italic (Boolean) (defaults to: false)
  • href (String) (defaults to: nil)


10
11
12
13
14
15
16
# File 'lib/article_json/elements/text.rb', line 10

def initialize(content:, bold: false, italic: false, href: nil)
  @type = :text
  @content = content
  @bold = bold
  @italic = italic
  @href = href
end

Instance Attribute Details

#boldObject (readonly)

Returns the value of attribute bold.



4
5
6
# File 'lib/article_json/elements/text.rb', line 4

def bold
  @bold
end

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'lib/article_json/elements/text.rb', line 4

def content
  @content
end

#hrefObject (readonly)

Returns the value of attribute href.



4
5
6
# File 'lib/article_json/elements/text.rb', line 4

def href
  @href
end

#italicObject (readonly)

Returns the value of attribute italic.



4
5
6
# File 'lib/article_json/elements/text.rb', line 4

def italic
  @italic
end

Class Method Details

.parse_hash(hash) ⇒ ArticleJSON::Elements::Text

Create a text element from Hash



54
55
56
57
58
59
60
61
# File 'lib/article_json/elements/text.rb', line 54

def parse_hash(hash)
  new(
    content: hash[:content],
    bold: hash[:bold],
    italic: hash[:italic],
    href: hash[:href]
  )
end

Instance Method Details

#blank?Boolean

Returns ‘true` if `content` is empty (see `#empty?`) or only contains whitespace (including non-breaking whitespace) characters

Returns:

  • (Boolean)


39
40
41
# File 'lib/article_json/elements/text.rb', line 39

def blank?
  empty? || content.gsub(/[\s\u00A0]/, '').empty?
end

#empty?Boolean

Returns ‘true` if `content` has a length of zero or is `nil`

Returns:

  • (Boolean)


32
33
34
# File 'lib/article_json/elements/text.rb', line 32

def empty?
  !content || content.empty?
end

#lengthInteger Also known as: size

Get the number of characters contained by this text element

Returns:

  • (Integer)


45
46
47
48
# File 'lib/article_json/elements/text.rb', line 45

def length
  return 0 if blank?
  content.length
end

#to_hHash

Hash representation of this text node

Returns:

  • (Hash)


20
21
22
23
24
25
26
27
28
# File 'lib/article_json/elements/text.rb', line 20

def to_h
  {
    type: type,
    content: content,
    bold: bold,
    italic: italic,
    href: href,
  }
end