caracal 1.0.6 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -2
- data/lib/caracal/core/models/page_size_model.rb +34 -25
- data/lib/caracal/core/page_settings.rb +18 -16
- data/lib/caracal/renderers/document_renderer.rb +3 -2
- data/lib/caracal/version.rb +1 -1
- data/spec/lib/caracal/core/models/page_size_model_spec.rb +42 -32
- data/spec/lib/caracal/core/page_settings_spec.rb +33 -25
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8aeb4bbbf23d57b58d105f3cb14e93721476dccb
|
4
|
+
data.tar.gz: 24a30fd6c194b1ce00d29277a62937c956307494
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 22d42de924ae632ff309b1c74254331d34ab37502a8115960964277657c5b655342439bac025f7ac3b9465126ee3ac1214bc2d8ba6b81f06576eed6d7ffd7feb
|
7
|
+
data.tar.gz: 75e87e2efd8f69ed0803316c5f84fb56812fb768457b6ac4a6d91ce0a16ea6e673f84ba42836314ae8fe3c4253e9998050d51a1bb022a934f9fa0fc03475d53e
|
data/README.md
CHANGED
@@ -256,13 +256,16 @@ docx.name # => 'example_document.docx'
|
|
256
256
|
### Page Size
|
257
257
|
|
258
258
|
Page dimensions can be set using the `page_size` method. The method accepts two parameters for controlling the width and height of the document.
|
259
|
+
It also accepts a third parameter for setting the page orientation. If you want landscape orientation, you need to change both the page
|
260
|
+
dimensions and the orientation explicitly.
|
259
261
|
|
260
262
|
*Page size defaults to United States standard A4, portrait dimensions (8.5in x 11in).*
|
261
263
|
|
262
264
|
```ruby
|
263
265
|
docx.page_size do
|
264
|
-
width
|
265
|
-
height
|
266
|
+
width 12240 # sets the page width. units in twips.
|
267
|
+
height 15840 # sets the page height. units in twips.
|
268
|
+
orientation :landscape # sets the printer orientation. accepts :portrait and :landscape.
|
266
269
|
end
|
267
270
|
```
|
268
271
|
|
@@ -4,67 +4,76 @@ require 'caracal/core/models/base_model'
|
|
4
4
|
module Caracal
|
5
5
|
module Core
|
6
6
|
module Models
|
7
|
-
|
7
|
+
|
8
8
|
# This class handles block options passed to the page size
|
9
9
|
# method.
|
10
10
|
#
|
11
11
|
class PageSizeModel < BaseModel
|
12
|
-
|
12
|
+
|
13
13
|
#-------------------------------------------------------------
|
14
14
|
# Configuration
|
15
15
|
#-------------------------------------------------------------
|
16
|
-
|
16
|
+
|
17
17
|
# constants
|
18
|
-
const_set(:DEFAULT_PAGE_WIDTH,
|
19
|
-
const_set(:DEFAULT_PAGE_HEIGHT,
|
20
|
-
|
18
|
+
const_set(:DEFAULT_PAGE_WIDTH, 12240) # 8.5in in twips
|
19
|
+
const_set(:DEFAULT_PAGE_HEIGHT, 15840) # 11.0in in twips
|
20
|
+
const_set(:DEFAULT_PAGE_ORIENTATION, 'portrait')
|
21
|
+
|
21
22
|
# accessors
|
22
23
|
attr_reader :page_width
|
23
24
|
attr_reader :page_height
|
24
|
-
|
25
|
+
attr_reader :page_orientation
|
26
|
+
|
25
27
|
# initialization
|
26
28
|
def initialize(options={}, &block)
|
27
|
-
@page_width
|
28
|
-
@page_height
|
29
|
-
|
29
|
+
@page_width = DEFAULT_PAGE_WIDTH
|
30
|
+
@page_height = DEFAULT_PAGE_HEIGHT
|
31
|
+
@page_orientation = DEFAULT_PAGE_ORIENTATION
|
32
|
+
|
30
33
|
super options, &block
|
31
34
|
end
|
32
|
-
|
33
|
-
|
35
|
+
|
36
|
+
|
34
37
|
#-------------------------------------------------------------
|
35
38
|
# Public Methods
|
36
39
|
#-------------------------------------------------------------
|
37
|
-
|
40
|
+
|
38
41
|
#=============== SETTERS ==============================
|
39
|
-
|
42
|
+
|
40
43
|
def height(value)
|
41
44
|
@page_height = value.to_i
|
42
45
|
end
|
43
|
-
|
46
|
+
|
47
|
+
def orientation(value)
|
48
|
+
allowed = ['landscape','portrait']
|
49
|
+
given = value.to_s.downcase
|
50
|
+
@page_orientation = allowed.include?(given) ? given : 'portrait'
|
51
|
+
end
|
52
|
+
|
44
53
|
def width(value)
|
45
54
|
@page_width = value.to_i
|
46
55
|
end
|
47
|
-
|
48
|
-
|
56
|
+
|
57
|
+
|
49
58
|
#=============== VALIDATION ==============================
|
50
|
-
|
59
|
+
|
51
60
|
def valid?
|
52
61
|
dims = [page_width, page_height]
|
53
62
|
dims.all? { |d| d > 0 }
|
54
63
|
end
|
55
|
-
|
56
|
-
|
64
|
+
|
65
|
+
|
57
66
|
#-------------------------------------------------------------
|
58
67
|
# Private Instance Methods
|
59
68
|
#-------------------------------------------------------------
|
60
69
|
private
|
61
|
-
|
70
|
+
|
62
71
|
def option_keys
|
63
|
-
[:width, :height]
|
72
|
+
[:width, :height, :orientation]
|
64
73
|
end
|
65
|
-
|
74
|
+
|
66
75
|
end
|
67
|
-
|
76
|
+
|
68
77
|
end
|
69
78
|
end
|
70
|
-
end
|
79
|
+
end
|
@@ -5,32 +5,33 @@ require 'caracal/errors'
|
|
5
5
|
|
6
6
|
module Caracal
|
7
7
|
module Core
|
8
|
-
|
9
|
-
# This module encapsulates all the functionality related to setting the
|
8
|
+
|
9
|
+
# This module encapsulates all the functionality related to setting the
|
10
10
|
# document's size and margins.
|
11
11
|
#
|
12
12
|
module PageSettings
|
13
13
|
def self.included(base)
|
14
14
|
base.class_eval do
|
15
|
-
|
15
|
+
|
16
16
|
#-------------------------------------------------------------
|
17
17
|
# Configuration
|
18
18
|
#-------------------------------------------------------------
|
19
|
-
|
19
|
+
|
20
20
|
# accessors
|
21
21
|
attr_reader :page_width
|
22
|
+
attr_reader :page_orientation
|
22
23
|
attr_reader :page_height
|
23
24
|
attr_reader :page_margin_top
|
24
25
|
attr_reader :page_margin_bottom
|
25
26
|
attr_reader :page_margin_left
|
26
27
|
attr_reader :page_margin_right
|
27
|
-
|
28
|
-
|
28
|
+
|
29
|
+
|
29
30
|
#-------------------------------------------------------------
|
30
31
|
# Public Methods
|
31
32
|
#-------------------------------------------------------------
|
32
|
-
|
33
|
-
# This method controls the physical margins of the printed page. Defaults
|
33
|
+
|
34
|
+
# This method controls the physical margins of the printed page. Defaults
|
34
35
|
# to 1in on each side.
|
35
36
|
#
|
36
37
|
def page_margins(options={}, &block)
|
@@ -49,24 +50,25 @@ module Caracal
|
|
49
50
|
raise Caracal::Errors::InvalidModelError, 'page_margins method requires non-zero :top, :bottom, :left, and :right options.'
|
50
51
|
end
|
51
52
|
end
|
52
|
-
|
53
|
-
# This method controls the physical width and height of the printed page. Defaults
|
53
|
+
|
54
|
+
# This method controls the physical width and height of the printed page. Defaults
|
54
55
|
# to US standard A4 portrait size.
|
55
56
|
#
|
56
57
|
def page_size(options={}, &block)
|
57
58
|
model = Caracal::Core::Models::PageSizeModel.new(options, &block)
|
58
|
-
|
59
|
+
|
59
60
|
if model.valid?
|
60
|
-
@page_width
|
61
|
-
@page_height
|
61
|
+
@page_width = model.page_width
|
62
|
+
@page_height = model.page_height
|
63
|
+
@page_orientation = model.page_orientation
|
62
64
|
else
|
63
65
|
raise Caracal::Errors::InvalidModelError, 'page_size method requires non-zero :width and :height options.'
|
64
66
|
end
|
65
67
|
end
|
66
|
-
|
68
|
+
|
67
69
|
end
|
68
70
|
end
|
69
71
|
end
|
70
|
-
|
72
|
+
|
71
73
|
end
|
72
|
-
end
|
74
|
+
end
|
@@ -357,8 +357,9 @@ module Caracal
|
|
357
357
|
|
358
358
|
def page_size_options
|
359
359
|
{
|
360
|
-
'w:w'
|
361
|
-
'w:h'
|
360
|
+
'w:w' => document.page_width,
|
361
|
+
'w:h' => document.page_height,
|
362
|
+
'w:orient' => document.page_orientation
|
362
363
|
}
|
363
364
|
end
|
364
365
|
|
data/lib/caracal/version.rb
CHANGED
@@ -1,59 +1,69 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Caracal::Core::Models::PageSizeModel do
|
4
|
-
subject do
|
4
|
+
subject do
|
5
5
|
described_class.new do
|
6
6
|
width 15840
|
7
7
|
height 12240
|
8
8
|
end
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
#-------------------------------------------------------------
|
12
12
|
# Configuration
|
13
13
|
#-------------------------------------------------------------
|
14
14
|
|
15
15
|
describe 'configuration tests' do
|
16
|
-
|
16
|
+
|
17
17
|
# constants
|
18
18
|
describe 'constants' do
|
19
19
|
it { expect(described_class::DEFAULT_PAGE_WIDTH).to eq 12240 }
|
20
20
|
it { expect(described_class::DEFAULT_PAGE_HEIGHT).to eq 15840 }
|
21
|
+
it { expect(described_class::DEFAULT_PAGE_ORIENTATION).to eq 'portrait' }
|
21
22
|
end
|
22
|
-
|
23
|
+
|
23
24
|
# accessors
|
24
25
|
describe 'accessors' do
|
25
26
|
it { expect(subject.page_width).to eq 15840 }
|
26
27
|
it { expect(subject.page_height).to eq 12240 }
|
27
28
|
end
|
28
|
-
|
29
|
+
|
29
30
|
end
|
30
|
-
|
31
|
-
|
31
|
+
|
32
|
+
|
32
33
|
#-------------------------------------------------------------
|
33
34
|
# Public Methods
|
34
35
|
#-------------------------------------------------------------
|
35
|
-
|
36
|
+
|
36
37
|
describe 'public method tests' do
|
37
|
-
|
38
|
+
|
38
39
|
#=============== SETTERS ==========================
|
39
|
-
|
40
|
-
# .width
|
41
|
-
describe '.width' do
|
42
|
-
before { subject.width(10000) }
|
43
|
-
|
44
|
-
it { expect(subject.page_width).to eq 10000 }
|
45
|
-
end
|
46
|
-
|
40
|
+
|
47
41
|
# .height
|
48
42
|
describe '.height' do
|
49
43
|
before { subject.height(10000) }
|
50
|
-
|
44
|
+
|
51
45
|
it { expect(subject.page_height).to eq 10000 }
|
52
46
|
end
|
53
|
-
|
54
|
-
|
47
|
+
|
48
|
+
# .orientation
|
49
|
+
describe '.orientation' do
|
50
|
+
before { subject.orientation('landscape') }
|
51
|
+
|
52
|
+
it { expect(subject.page_orientation).to eq 'landscape' }
|
53
|
+
end
|
54
|
+
|
55
|
+
# .width
|
56
|
+
describe '.width' do
|
57
|
+
before { subject.width(10000) }
|
58
|
+
|
59
|
+
it { expect(subject.page_width).to eq 10000 }
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
55
65
|
#=============== VALIDATION ===========================
|
56
|
-
|
66
|
+
|
57
67
|
describe '.valid?' do
|
58
68
|
describe 'when all sizes gt 0' do
|
59
69
|
it { expect(subject.valid?).to eq true }
|
@@ -63,29 +73,29 @@ describe Caracal::Core::Models::PageSizeModel do
|
|
63
73
|
before do
|
64
74
|
allow(subject).to receive("page_#{ d }").and_return(0)
|
65
75
|
end
|
66
|
-
|
76
|
+
|
67
77
|
it { expect(subject.valid?).to eq false }
|
68
78
|
end
|
69
79
|
end
|
70
80
|
end
|
71
|
-
|
81
|
+
|
72
82
|
end
|
73
|
-
|
74
|
-
|
83
|
+
|
84
|
+
|
75
85
|
#-------------------------------------------------------------
|
76
86
|
# Private Methods
|
77
87
|
#-------------------------------------------------------------
|
78
|
-
|
88
|
+
|
79
89
|
describe 'private method tests' do
|
80
|
-
|
90
|
+
|
81
91
|
# .option_keys
|
82
92
|
describe '.option_keys' do
|
83
93
|
let(:actual) { subject.send(:option_keys).sort }
|
84
|
-
let(:expected) { [:width, :height].sort }
|
85
|
-
|
94
|
+
let(:expected) { [:width, :height, :orientation].sort }
|
95
|
+
|
86
96
|
it { expect(actual).to eq expected }
|
87
97
|
end
|
88
|
-
|
98
|
+
|
89
99
|
end
|
90
|
-
|
91
|
-
end
|
100
|
+
|
101
|
+
end
|
@@ -2,18 +2,19 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Caracal::Core::PageSettings do
|
4
4
|
subject { Caracal::Document.new }
|
5
|
-
|
6
|
-
|
5
|
+
|
6
|
+
|
7
7
|
#-------------------------------------------------------------
|
8
8
|
# Configuration
|
9
9
|
#-------------------------------------------------------------
|
10
10
|
|
11
11
|
describe 'configuration tests' do
|
12
|
-
|
12
|
+
|
13
13
|
# readers
|
14
14
|
describe 'page size readers' do
|
15
15
|
it { expect(subject.page_width).to eq 12240 }
|
16
16
|
it { expect(subject.page_height).to eq 15840 }
|
17
|
+
it { expect(subject.page_orientation).to eq 'portrait' }
|
17
18
|
end
|
18
19
|
describe 'page margin readers' do
|
19
20
|
it { expect(subject.page_margin_top).to eq 1440 }
|
@@ -21,67 +22,74 @@ describe Caracal::Core::PageSettings do
|
|
21
22
|
it { expect(subject.page_margin_left).to eq 1440 }
|
22
23
|
it { expect(subject.page_margin_right).to eq 1440 }
|
23
24
|
end
|
24
|
-
|
25
|
+
|
25
26
|
end
|
26
|
-
|
27
|
-
|
27
|
+
|
28
|
+
|
28
29
|
#-------------------------------------------------------------
|
29
30
|
# Public Methods
|
30
31
|
#-------------------------------------------------------------
|
31
32
|
|
32
33
|
describe 'public methods tests' do
|
33
|
-
|
34
|
+
|
34
35
|
# .page_size
|
35
36
|
describe '.page_size' do
|
36
37
|
describe 'when options given' do
|
37
|
-
before { subject.page_size width: 10000, height: 12000 }
|
38
|
-
|
38
|
+
before { subject.page_size width: 10000, height: 12000, orientation: :landscape }
|
39
|
+
|
39
40
|
it { expect(subject.page_width).to eq 10000 }
|
40
41
|
it { expect(subject.page_height).to eq 12000 }
|
42
|
+
it { expect(subject.page_orientation).to eq 'landscape' }
|
41
43
|
end
|
42
44
|
describe 'when block given' do
|
43
45
|
before do
|
44
46
|
subject.page_size do
|
45
|
-
width
|
46
|
-
height
|
47
|
+
width 10000
|
48
|
+
height 12000
|
49
|
+
orientation :landscape
|
47
50
|
end
|
48
51
|
end
|
49
|
-
|
52
|
+
|
50
53
|
it { expect(subject.page_width).to eq 10000 }
|
51
54
|
it { expect(subject.page_height).to eq 12000 }
|
55
|
+
it { expect(subject.page_orientation).to eq 'landscape' }
|
52
56
|
end
|
53
57
|
describe 'when fancy block given' do
|
54
58
|
subject do
|
55
59
|
Caracal::Document.new do |docx|
|
56
60
|
w = 10000
|
57
61
|
h = 12000
|
62
|
+
o = :landscape
|
58
63
|
docx.page_size do
|
59
|
-
width
|
60
|
-
height
|
64
|
+
width w
|
65
|
+
height h
|
66
|
+
orientation o
|
61
67
|
end
|
62
68
|
end
|
63
69
|
end
|
64
|
-
|
70
|
+
|
65
71
|
it { expect(subject.page_width).to eq 10000 }
|
66
72
|
it { expect(subject.page_height).to eq 12000 }
|
73
|
+
it { expect(subject.page_orientation).to eq 'landscape' }
|
67
74
|
end
|
68
75
|
describe 'when both given' do
|
69
76
|
before do
|
70
|
-
subject.page_size width: 10000 do
|
77
|
+
subject.page_size width: 10000, orientation: :landscape do
|
71
78
|
height 12000
|
72
79
|
end
|
73
80
|
end
|
74
|
-
|
81
|
+
|
75
82
|
it { expect(subject.page_width).to eq 10000 }
|
76
83
|
it { expect(subject.page_height).to eq 12000 }
|
84
|
+
it { expect(subject.page_orientation).to eq 'landscape' }
|
77
85
|
end
|
78
86
|
end
|
79
|
-
|
87
|
+
|
80
88
|
# .page_margins
|
81
89
|
describe '.page_margins' do
|
82
90
|
describe 'when options given' do
|
83
91
|
before { subject.page_margins top: 1441, bottom: 1442, left: 1443, right: 1444 }
|
84
|
-
|
92
|
+
|
85
93
|
it { expect(subject.page_margin_top).to eq 1441 }
|
86
94
|
it { expect(subject.page_margin_bottom).to eq 1442 }
|
87
95
|
it { expect(subject.page_margin_left).to eq 1443 }
|
@@ -96,7 +104,7 @@ describe Caracal::Core::PageSettings do
|
|
96
104
|
right 1444
|
97
105
|
end
|
98
106
|
end
|
99
|
-
|
107
|
+
|
100
108
|
it { expect(subject.page_margin_top).to eq 1441 }
|
101
109
|
it { expect(subject.page_margin_bottom).to eq 1442 }
|
102
110
|
it { expect(subject.page_margin_left).to eq 1443 }
|
@@ -117,7 +125,7 @@ describe Caracal::Core::PageSettings do
|
|
117
125
|
end
|
118
126
|
end
|
119
127
|
end
|
120
|
-
|
128
|
+
|
121
129
|
it { expect(subject.page_margin_top).to eq 1441 }
|
122
130
|
it { expect(subject.page_margin_bottom).to eq 1442 }
|
123
131
|
it { expect(subject.page_margin_left).to eq 1443 }
|
@@ -130,14 +138,14 @@ describe Caracal::Core::PageSettings do
|
|
130
138
|
right 1444
|
131
139
|
end
|
132
140
|
end
|
133
|
-
|
141
|
+
|
134
142
|
it { expect(subject.page_margin_top).to eq 1441 }
|
135
143
|
it { expect(subject.page_margin_bottom).to eq 1442 }
|
136
144
|
it { expect(subject.page_margin_left).to eq 1443 }
|
137
145
|
it { expect(subject.page_margin_right).to eq 1444 }
|
138
146
|
end
|
139
147
|
end
|
140
|
-
|
148
|
+
|
141
149
|
end
|
142
|
-
|
143
|
-
end
|
150
|
+
|
151
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: caracal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Trade Infomatics
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-09-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: nokogiri
|