Module: Caracal::Core::Tables

Included in:
Document
Defined in:
lib/caracal/core/tables.rb

Overview

This module encapsulates all the functionality related to adding tables to the document.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/caracal/core/tables.rb', line 12

def self.included(base)
  base.class_eval do
    
    #-------------------------------------------------------------
    # Public Methods
    #-------------------------------------------------------------
    
    def table(*args, &block)
      options = Caracal::Utilities.extract_options!(args)
      options.merge!({ data: args.first }) if args.first
      
      model = Caracal::Core::Models::TableModel.new(options, &block)
      if respond_to?(:page_width)
        container_width = page_width - page_margin_left - page_margin_right
        model.calculate_width(container_width)
      end
      
      if model.valid?
        contents << model
      else
        raise Caracal::Errors::InvalidModelError, 'Table must be provided data for at least one cell.'
      end
      model
    end
    
  end
end