Module: Caracal::Core::PageSettings

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

Overview

This module encapsulates all the functionality related to setting the document’s size and margins.

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/caracal/core/page_settings.rb', line 13

def self.included(base)
  base.class_eval do
    
    #-------------------------------------------------------------
    # Configuration
    #-------------------------------------------------------------
    
    # accessors
    attr_reader :page_width
    attr_reader :page_height
    attr_reader :page_margin_top
    attr_reader :page_margin_bottom
    attr_reader :page_margin_left
    attr_reader :page_margin_right
    
    
    #-------------------------------------------------------------
    # Public Methods
    #-------------------------------------------------------------
    
    # This method controls the physical margins of the printed page. Defaults 
    # to 1in on each side.
    #
    def page_margins(options={}, &block)
      model = Caracal::Core::Models::MarginModel.new(options, &block)

      if model.valid?
        if (model.margin_top + model.margin_bottom < page_height) && (model.margin_left + model.margin_right < page_width)
          @page_margin_top    = model.margin_top
          @page_margin_bottom = model.margin_bottom
          @page_margin_left   = model.margin_left
          @page_margin_right  = model.margin_right
        else
          raise Caracal::Errors::InvalidModelError, 'page_margins method requires margins to be smaller than the page size.'
        end
      else
        raise Caracal::Errors::InvalidModelError, 'page_margins method requires non-zero :top, :bottom, :left, and :right options.'
      end
    end
    
    # This method controls the physical width and height of the printed page. Defaults 
    # to US standard A4 portrait size.
    #
    def page_size(options={}, &block)
      model = Caracal::Core::Models::PageSizeModel.new(options, &block)
      
      if model.valid?
        @page_width  = model.page_width
        @page_height = model.page_height
      else
        raise Caracal::Errors::InvalidModelError, 'page_size method requires non-zero :width and :height options.'
      end
    end
    
  end
end