Class: ArticleJSON::Elements::Paragraph

Inherits:
Base
  • Object
show all
Defined in:
lib/article_json/elements/paragraph.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:) ⇒ Paragraph

Returns a new instance of Paragraph.

Parameters:



7
8
9
10
# File 'lib/article_json/elements/paragraph.rb', line 7

def initialize(content:)
  @type = :paragraph
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

Class Method Details

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

Create a paragraph element from Hash



48
49
50
# File 'lib/article_json/elements/paragraph.rb', line 48

def parse_hash(hash)
  new(content: parse_hash_list(hash[:content]))
end

Instance Method Details

#blank?Boolean

Return ‘true` if the paragraph is empty or if all elements are blank

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/article_json/elements/paragraph.rb', line 29

def blank?
  empty? || content.all? do |element|
    element.respond_to?(:blank?) && element.blank?
  end
end

#empty?Boolean

Return ‘true` if the paragraph has no elements

Returns:

  • (Boolean)


23
24
25
# File 'lib/article_json/elements/paragraph.rb', line 23

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

#lengthInteger Also known as: size

Return the sum of all characters within the content’s text elements

Returns:

  • (Integer)


37
38
39
40
41
42
# File 'lib/article_json/elements/paragraph.rb', line 37

def length
  return 0 if empty?
  @content.reduce(0) do |sum, element|
    sum + (element.respond_to?(:length) ? element.length : 0)
  end
end

#to_hHash

Hash representation of this heading element

Returns:

  • (Hash)


14
15
16
17
18
19
# File 'lib/article_json/elements/paragraph.rb', line 14

def to_h
  {
    type: type,
    content: content.map(&:to_h),
  }
end