Class: ArticleJSON::Elements::Paragraph
- Defined in:
- lib/article_json/elements/paragraph.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
Attributes inherited from Base
Class Method Summary collapse
-
.parse_hash(hash) ⇒ ArticleJSON::Elements::Paragraph
Create a paragraph element from Hash.
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Return ‘true` if the paragraph is empty or if all elements are blank.
-
#empty? ⇒ Boolean
Return ‘true` if the paragraph has no elements.
-
#initialize(content:) ⇒ Paragraph
constructor
A new instance of Paragraph.
-
#length ⇒ Integer
(also: #size)
Return the sum of all characters within the content’s text elements.
-
#to_h ⇒ Hash
Hash representation of this heading element.
Methods inherited from Base
Constructor Details
#initialize(content:) ⇒ Paragraph
Returns a new instance of Paragraph.
7 8 9 10 |
# File 'lib/article_json/elements/paragraph.rb', line 7 def initialize(content:) @type = :paragraph @content = content end |
Instance Attribute Details
#content ⇒ Object (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
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
23 24 25 |
# File 'lib/article_json/elements/paragraph.rb', line 23 def empty? !content || content.empty? end |
#length ⇒ Integer Also known as: size
Return the sum of all characters within the content’s text elements
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_h ⇒ Hash
Hash representation of this heading element
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 |