Class: Uniword::Builder::CommentBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/builder/comment_builder.rb

Overview

Builds and configures Comment objects.

Examples:

Create a comment

comment = CommentBuilder.new(author: 'Editor')
comment << 'This needs review'
comment.build

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil, author: nil, comment_id: nil, date: nil, initials: nil) ⇒ CommentBuilder

Returns a new instance of CommentBuilder.



14
15
16
17
18
19
20
21
# File 'lib/uniword/builder/comment_builder.rb', line 14

def initialize(model: nil, author: nil, comment_id: nil, date: nil, initials: nil)
  @model = model || Comment.new(
    author: author,
    comment_id: comment_id,
    date: date,
    initials: initials,
  )
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



12
13
14
# File 'lib/uniword/builder/comment_builder.rb', line 12

def model
  @model
end

Class Method Details

.from_model(model) ⇒ Object

Wrap an existing Comment model



24
25
26
# File 'lib/uniword/builder/comment_builder.rb', line 24

def self.from_model(model)
  new(model: model)
end

Instance Method Details

#<<(element) ⇒ self

Append text to the comment (creates a paragraph)

Parameters:

  • element (String, Paragraph)

Returns:

  • (self)


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/uniword/builder/comment_builder.rb', line 32

def <<(element)
  case element
  when String
    para = Wordprocessingml::Paragraph.new
    para.runs << Wordprocessingml::Run.new(text: element)
    @model.paragraphs << para
  when Wordprocessingml::Paragraph
    @model.paragraphs << element
  else
    raise ArgumentError, "Cannot add #{element.class} to comment"
  end
  self
end

#buildObject

Return the underlying Comment model



47
48
49
# File 'lib/uniword/builder/comment_builder.rb', line 47

def build
  @model
end