Class: Caracal::Core::Models::TableCellModel

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/caracal/core/models/table_cell_model.rb

Overview

This class handles block options passed to tables via their data collections.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ TableCellModel

initialization



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/caracal/core/models/table_cell_model.rb', line 33

def initialize(options={}, &block)
  @cell_background      = DEFAULT_CELL_BACKGROUND
  @cell_margins         = DEFAULT_CELL_MARGINS
  @cell_vertical_align  = DEFAULT_CELL_VERTICAL_ALIGN

  if content = options.delete(:content)
    p content, options.dup, &block
  end

  super options, &block

  p_klass = Caracal::Core::Models::ParagraphModel     # the final tag in a table cell
  unless contents.last.is_a? p_klass                  # *must* be a paragraph for OOXML
    contents << p_klass.new(content: '')              # to not throw an error.
  end
end

Instance Attribute Details

#cell_backgroundObject (readonly)

accessors



25
26
27
# File 'lib/caracal/core/models/table_cell_model.rb', line 25

def cell_background
  @cell_background
end

#cell_colspanObject (readonly)

Returns the value of attribute cell_colspan.



30
31
32
# File 'lib/caracal/core/models/table_cell_model.rb', line 30

def cell_colspan
  @cell_colspan
end

#cell_marginsObject (readonly)

Returns the value of attribute cell_margins.



27
28
29
# File 'lib/caracal/core/models/table_cell_model.rb', line 27

def cell_margins
  @cell_margins
end

#cell_rowspanObject (readonly)

Returns the value of attribute cell_rowspan.



29
30
31
# File 'lib/caracal/core/models/table_cell_model.rb', line 29

def cell_rowspan
  @cell_rowspan
end

#cell_vertical_alignObject (readonly)

Returns the value of attribute cell_vertical_align.



28
29
30
# File 'lib/caracal/core/models/table_cell_model.rb', line 28

def cell_vertical_align
  @cell_vertical_align
end

#cell_widthObject (readonly)

Returns the value of attribute cell_width.



26
27
28
# File 'lib/caracal/core/models/table_cell_model.rb', line 26

def cell_width
  @cell_width
end

Instance Method Details

#apply_styles(opts = {}) ⇒ Object

This method allows styles to be applied to this cell from the table level. It attempts to add the style first to the instance, and then to any sub-models that respond to the method.

In all cases, invalid options will simply be ignored.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/caracal/core/models/table_cell_model.rb', line 71

def apply_styles(opts={})
  # make dup of options so we don't
  # harm args sent to sibling cells
  options = opts.dup

  # first, try apply to self
  options.each do |(k,v)|
    send(k, v)  if respond_to?(k)
  end

  # prevent top-level attrs from trickling down
  options.delete_if { |(k,v)| option_keys.include?(k) }

  # then, try apply to contents
  contents.each do |model|
    options.each do |k,v|
      model.send(k, v) if model.respond_to?(k)
    end

    # finally, apply to runs. options do trickle down
    # because paragraph-level styles don't seem to
    # affect runs within tables. weirdsies.
    if model.respond_to?(:runs)
      model.runs.each do |run|
        options.each do |k,v|
          run.send(k, v) if run.respond_to?(k)
        end
      end
    end
  end
end

#calculate_width(default_width) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/caracal/core/models/table_cell_model.rb', line 103

def calculate_width(default_width)
  width(default_width) unless cell_width.to_i > 0

  container_width = cell_width - cell_margin_left - cell_margin_right

  contents.each do |model|
    if model.respond_to?(:calculate_width)
      model.calculate_width(container_width)    # will always be a TableModel
    end
  end
end

#contentsObject

DATA ACCESSORS =======================


57
58
59
# File 'lib/caracal/core/models/table_cell_model.rb', line 57

def contents
  @contents ||= []
end

#valid?Boolean

VALIDATION ===========================

Returns:

  • (Boolean)


158
159
160
# File 'lib/caracal/core/models/table_cell_model.rb', line 158

def valid?
  contents.size > 0
end