class Caracal::Core::Models::ParagraphModel

This class encapsulates the logic needed to store and manipulate paragraph data.

Attributes

paragraph_align[R]
paragraph_bgcolor[R]
paragraph_bold[R]
paragraph_color[R]
paragraph_italic[R]
paragraph_keep_next[R]
paragraph_size[R]
paragraph_style[R]

readers

paragraph_underline[R]

Public Class Methods

new(options={}, &block) click to toggle source

initialization

Calls superclass method Caracal::Core::Models::BaseModel::new
# File lib/caracal/core/models/paragraph_model.rb, line 33
def initialize(options={}, &block)
  content = options.delete(:content) { "" }
  text content, options.dup, &block
  @indent = nil
  super options, &block
end

Public Instance Methods

bookmark_end(*args, &block) click to toggle source
# File lib/caracal/core/models/paragraph_model.rb, line 126
def bookmark_end(*args, &block)
  options = Caracal::Utilities.extract_options!(args)
  options.merge!({ start: false})

  model = Caracal::Core::Models::BookmarkModel.new(options, &block)
  if model.valid?
    runs << model
  else
    raise Caracal::Errors::InvalidModelError, 'Bookmark ending tags require an id.'
  end
  model
end
bookmark_start(*args, &block) click to toggle source

.bookmarks

# File lib/caracal/core/models/paragraph_model.rb, line 114
def bookmark_start(*args, &block)
  options = Caracal::Utilities.extract_options!(args)
  options.merge!({ start: true})

  model = Caracal::Core::Models::BookmarkModel.new(options, &block)
  if model.valid?
    runs << model
  else
    raise Caracal::Errors::InvalidModelError, 'Bookmark starting tags require an id and a name.'
  end
  model
end
br() click to toggle source

.br

# File lib/caracal/core/models/paragraph_model.rb, line 140
def br
  model = Caracal::Core::Models::LineBreakModel.new()
  runs << model
  model
end
empty?() click to toggle source
STATE =================================
# File lib/caracal/core/models/paragraph_model.rb, line 184
def empty?
  runs.size.zero? || plain_text.length.zero?
end
indent(hash = nil) click to toggle source

Getter/setter

# File lib/caracal/core/models/paragraph_model.rb, line 100
def indent(hash = nil)
  return @indent if hash.nil?

  unless [:left, :right].include?(hash.keys.first) && hash.values.first.is_a?(Integer)
    raise Caracal::Errors::InvalidModelError, 'the indent setter requires a hash like left: X or right: Y.'
  end

  @indent = { side: hash.keys.first, value: hash.values.first }
end
page() click to toggle source

.page

# File lib/caracal/core/models/paragraph_model.rb, line 162
def page
  model = Caracal::Core::Models::PageBreakModel.new({ wrap: false })
  runs << model
  model
end
plain_text() click to toggle source

.plaintext

# File lib/caracal/core/models/paragraph_model.rb, line 65
def plain_text
  runs.collect { |run| run.try(:text_content).to_s }.join(' ').strip
end
run_attributes() click to toggle source

.run_attributes

# File lib/caracal/core/models/paragraph_model.rb, line 53
def run_attributes
  {
    color:      paragraph_color,
    size:       paragraph_size,
    bold:       paragraph_bold,
    italic:     paragraph_italic,
    underline:  paragraph_underline,
    bgcolor:    paragraph_bgcolor
  }
end
runs() click to toggle source

.runs

# File lib/caracal/core/models/paragraph_model.rb, line 48
def runs
  @runs ||= []
end
text(*args, &block) click to toggle source

.text

# File lib/caracal/core/models/paragraph_model.rb, line 169
def text(*args, &block)
  options = Caracal::Utilities.extract_options!(args)
  options.merge!({ content: args.first }) if args.first

  model = Caracal::Core::Models::TextModel.new(options, &block)
  if model.valid?
    runs << model
  else
    raise Caracal::Errors::InvalidModelError, ':text method must receive a string for the display text.'
  end
  model
end
valid?() click to toggle source
VALIDATION ============================
# File lib/caracal/core/models/paragraph_model.rb, line 190
def valid?
  runs.size > 0
end

Private Instance Methods

option_keys() click to toggle source
# File lib/caracal/core/models/paragraph_model.rb, line 200
def option_keys
  [:content, :style, :align, :color, :size, :bold, :italic, :underline, :bgcolor]
end