Skip to content

Commit

Permalink
Merge pull request #2 from the-curve-consulting/mbarber/PWM-262
Browse files Browse the repository at this point in the history
PWM-262: Specify portrait and landscape page orientations in the same document
  • Loading branch information
jamesridgway authored Feb 2, 2024
2 parents 8c3b50b + 254e54a commit c9454b0
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/caracal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def self.root
include Caracal::Core::Text
end

Caracal::Core::Models::PageFlipModel.class_eval do
include Caracal::Core::Images
include Caracal::Core::Lists
include Caracal::Core::Rules
include Caracal::Core::Tables
include Caracal::Core::Text
end

Caracal::Core::Models::FooterModel.class_eval do
include Caracal::Core::Images
include Caracal::Core::Lists
Expand Down
41 changes: 41 additions & 0 deletions lib/caracal/core/models/page_flip_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'caracal/core/models/base_model'

module Caracal
module Core
module Models

# This class handles block options passed to tables via their data
# collections.
#
class PageFlipModel < BaseModel

#-------------------------------------------------------------
# Configuration
#-------------------------------------------------------------

# initialization
def initialize(options={}, &block)
super options, &block
end

#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

#=============== DATA ACCESSORS =======================

# .contents
def contents
@contents ||= []
end

#=============== VALIDATION ===========================

# .valid?
def valid?
contents.size > 0
end
end
end
end
end
34 changes: 34 additions & 0 deletions lib/caracal/core/page_flips.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'caracal/core/models/page_flip_model'
require 'caracal/errors'


module Caracal
module Core

# This module encapsulates all the functionality related to flipping
# specific page contents
#
module PageFlips
def self.included(base)
base.class_eval do

#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

def page_flip(*args, &block)
options = Caracal::Utilities.extract_options!(args)

model = Caracal::Core::Models::PageFlipModel.new(options, &block)

if model.valid?
contents << model
end

model
end
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/caracal/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require 'caracal/core/lists'
require 'caracal/core/namespaces'
require 'caracal/core/page_breaks'
require 'caracal/core/page_flips'
require 'caracal/core/page_numbers'
require 'caracal/core/page_settings'
require 'caracal/core/relationships'
Expand Down Expand Up @@ -61,6 +62,7 @@ class Document
include Caracal::Core::IFrames
include Caracal::Core::Images
include Caracal::Core::Lists
include Caracal::Core::PageFlips
include Caracal::Core::PageBreaks
include Caracal::Core::Rules
include Caracal::Core::Tables
Expand Down
29 changes: 28 additions & 1 deletion lib/caracal/renderers/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,33 @@ def render_listitem(xml, model, list_num)
end
end

def render_pageflip(xml, model)
xml['w'].p paragraph_options do
xml['w'].pPr do
xml['w'].sectPr do
xml['w'].pgSz({
'w:w' => document.page_width,
'w:h' => document.page_height
})
end
end
end
model.contents.each do |model|
method = render_method_for_model(model)
send(method, xml, model)
end
xml['w'].p paragraph_options do
xml['w'].pPr do
xml['w'].sectPr do
xml['w'].pgSz({
'w:w' => document.page_height,
'w:h' => document.page_width
})
end
end
end
end

def render_pagebreak(xml, model)
if model.page_break_wrap
xml['w'].p paragraph_options do
Expand Down Expand Up @@ -445,4 +472,4 @@ def page_size_options

end
end
end
end
34 changes: 34 additions & 0 deletions spec/lib/caracal/core/models/page_flip_model_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

describe Caracal::Core::Models::PageFlipModel do
let(:name) { 'Arial' }

subject { described_class.new }


#-------------------------------------------------------------
# Configuration
#-------------------------------------------------------------

describe 'configuration tests' do

describe 'inheritance' do
it { expect(subject).to be_a(Caracal::Core::Models::BaseModel) }
end
end

#=============== VALIDATION ===========================

describe '.valid?' do
describe 'when content provided' do
before { allow(subject).to receive(:contents).and_return(['a']) }

it { expect(subject.valid?).to eq true }
end
describe 'when content not provided' do
before { allow(subject).to receive(:contents).and_return([]) }

it { expect(subject.valid?).to eq false }
end
end
end
27 changes: 27 additions & 0 deletions spec/lib/caracal/core/page_flip_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

describe Caracal::Core::PageFlips do
subject { Caracal::Document.new }


#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

describe 'public method tests' do

# .page
describe '.page_flip' do
let!(:size) { subject.contents.size }

before do
subject.page_flip do |x|
x.p
end
end

it { expect(subject.contents.size).to eq size + 1 }
it { expect(subject.contents.last).to be_a(Caracal::Core::Models::PageFlipModel) }
end
end
end

0 comments on commit c9454b0

Please sign in to comment.