module Caracal::Core::PageSettings

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

Public Class Methods

included(base) click to toggle source
# 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_orientation
    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
        @page_orientation = model.page_orientation
      else
        raise Caracal::Errors::InvalidModelError, 'page_size method requires non-zero :width and :height options.'
      end
    end

  end
end

Public Instance Methods

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

This method controls the physical margins of the printed page. Defaults to 1in on each side.

# File lib/caracal/core/page_settings.rb, line 37
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
page_size(options={}, &block) click to toggle source

This method controls the physical width and height of the printed page. Defaults to US standard A4 portrait size.

# File lib/caracal/core/page_settings.rb, line 57
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
    @page_orientation = model.page_orientation
  else
    raise Caracal::Errors::InvalidModelError, 'page_size method requires non-zero :width and :height options.'
  end
end