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(author:, 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(author:, comment_id: nil, date: nil, initials: nil)
  @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
27
28
# File 'lib/uniword/builder/comment_builder.rb', line 24

def self.from_model(model)
  builder = allocate
  builder.instance_variable_set(:@model, model)
  builder
end

Instance Method Details

#<<(element) ⇒ self

Append text to the comment (creates a paragraph)

Parameters:

  • element (String, Paragraph)

Returns:

  • (self)


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

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



49
50
51
# File 'lib/uniword/builder/comment_builder.rb', line 49

def build
  @model
end