label_definitions 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/labels.yml CHANGED
@@ -18,7 +18,7 @@ Avery 5160:
18
18
  height: 25.4
19
19
  row_gutter: 0.0
20
20
  column_gutter: 3.175
21
- page_size: Letter
21
+ page_size: LETTER
22
22
  Avery 5395:
23
23
  rows: 4
24
24
  columns: 2
@@ -26,7 +26,7 @@ Avery 5395:
26
26
  height: 59.267
27
27
  row_gutter: 4.0527
28
28
  column_gutter: 9.5
29
- page_size: Letter
29
+ page_size: LETTER
30
30
  Avery 5418:
31
31
  rows: 9
32
32
  columns: 4
@@ -1,7 +1,6 @@
1
1
  module LabelDefinitions
2
2
  class Label
3
- attr_reader :name, :rows, :columns, :width, :height, :row_gutter, :column_gutter,
4
- :page_size, :page_width, :page_height
3
+ attr_reader :name, :rows, :columns, :width, :height, :row_gutter, :column_gutter
5
4
 
6
5
  def initialize(attributes = {})
7
6
  @name = attributes.fetch(:name)
@@ -11,14 +10,35 @@ module LabelDefinitions
11
10
  @height = attributes.fetch(:height).to_f
12
11
  @row_gutter = attributes.fetch(:row_gutter).to_f
13
12
  @column_gutter = attributes.fetch(:column_gutter).to_f
14
- if attributes.has_key? :page_size
15
- @page_size = attributes.fetch(:page_size)
16
- else
17
- @page_width = attributes.fetch(:page_width).to_f
18
- @page_height = attributes.fetch(:page_height).to_f
19
- end
13
+ @page_width = attributes.fetch(:page_width).to_f
14
+ @page_height = attributes.fetch(:page_height).to_f
20
15
  end
21
16
 
17
+ def page
18
+ @page ||= Page.new page_width, page_height, page_top_margin, page_left_margin
19
+ end
20
+
21
+ private
22
+
23
+ def page_width
24
+ @page_width
25
+ end
26
+
27
+ def page_height
28
+ @page_height
29
+ end
22
30
 
31
+ def page_top_margin
32
+ ( page_height - (rows * height) - ((rows-1) * row_gutter) ) / 2
33
+ end
34
+
35
+ def page_left_margin
36
+ ( page_width - (columns * width) - ((columns-1) * column_gutter) ) / 2
37
+ end
38
+
39
+ class Page < Struct.new(:width, :height, :top_margin, :left_margin)
40
+ alias :bottom_margin :top_margin
41
+ alias :right_margin :left_margin
42
+ end
23
43
  end
24
44
  end
@@ -0,0 +1,10 @@
1
+ module LabelDefinitions
2
+ module PageSize
3
+
4
+ SIZES = {
5
+ 'A4' => [210, 297],
6
+ 'LETTER' => [215.9, 279.4]
7
+ }
8
+
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module LabelDefinitions
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require 'yaml'
2
2
  require 'label_definitions/label'
3
+ require 'label_definitions/page_size'
3
4
  require 'label_definitions/version'
4
5
 
5
6
  module LabelDefinitions
@@ -10,6 +11,7 @@ module LabelDefinitions
10
11
  load_definitions.each do |name, definition|
11
12
  symbolize_keys! definition
12
13
  name_and_aliases(name, definition).each do |n|
14
+ convert_page_size! definition
13
15
  labels << Label.new(definition.merge({name: n}))
14
16
  end
15
17
  end
@@ -28,6 +30,14 @@ module LabelDefinitions
28
30
 
29
31
  private
30
32
 
33
+ def self.convert_page_size!(definition)
34
+ if (size = definition.delete :page_size)
35
+ width, height = LabelDefinitions::PageSize::SIZES[size]
36
+ definition[:page_width] = width
37
+ definition[:page_height] = height
38
+ end
39
+ end
40
+
31
41
  def self.name_and_aliases(name, definition)
32
42
  [name] + definition.fetch(:aliases, '').split(',').map(&:strip)
33
43
  end
@@ -38,6 +38,12 @@ class LabelDefinitionsTest < TestCase
38
38
  assert_equal count, LabelDefinitions.all.length
39
39
  end
40
40
 
41
+ test 'converts page size to width and height' do
42
+ label = LabelDefinitions.find 'Avery 5395'
43
+ assert_equal 215.9, label.page.width
44
+ assert_equal 279.4, label.page.height
45
+ end
46
+
41
47
  private
42
48
 
43
49
  def example_label
@@ -48,7 +54,8 @@ class LabelDefinitionsTest < TestCase
48
54
  height: 42,
49
55
  row_gutter: 0,
50
56
  column_gutter: 0,
51
- page_size: 'A4'
57
+ page_width: 210,
58
+ page_height: 297
52
59
  end
53
60
 
54
61
  end
data/test/label_test.rb CHANGED
@@ -3,22 +3,25 @@ require 'label_definitions/label'
3
3
 
4
4
  class LabelTest < TestCase
5
5
 
6
- test 'requires page size or (page width and height)' do
7
- assert_raises(KeyError) { LabelDefinitions::Label.new attributes }
8
- assert LabelDefinitions::Label.new(attributes.merge(page_size: 'A4'))
9
- assert LabelDefinitions::Label.new(attributes.merge(page_width: 42, page_height: 153))
6
+ test 'page' do
7
+ page = LabelDefinitions::Label.new(attributes.merge(page_width: 200, page_height: 300)).page
8
+ assert_equal 41.0, page.top_margin
9
+ assert_equal 41.0, page.bottom_margin
10
+
11
+ assert_equal 17.5, page.left_margin
12
+ assert_equal 17.5, page.right_margin
10
13
  end
11
14
 
12
15
  private
13
16
 
14
17
  def attributes
15
18
  { name: 'Some label',
16
- rows: 3,
17
- columns: 5,
18
- width: 153,
19
+ rows: 5,
20
+ columns: 3,
21
+ width: 53,
19
22
  height: 42,
20
- row_gutter: 0,
21
- column_gutter: 0 }
23
+ row_gutter: 2,
24
+ column_gutter: 3 }
22
25
  end
23
26
  end
24
27
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: label_definitions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-20 00:00:00.000000000 Z
12
+ date: 2013-11-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -75,6 +75,7 @@ files:
75
75
  - labels.yml
76
76
  - lib/label_definitions.rb
77
77
  - lib/label_definitions/label.rb
78
+ - lib/label_definitions/page_size.rb
78
79
  - lib/label_definitions/version.rb
79
80
  - test/helper.rb
80
81
  - test/label_definitions_test.rb
@@ -94,7 +95,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
95
  version: '0'
95
96
  segments:
96
97
  - 0
97
- hash: 3975846568932287638
98
+ hash: 4118092927290813753
98
99
  required_rubygems_version: !ruby/object:Gem::Requirement
99
100
  none: false
100
101
  requirements:
@@ -103,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
104
  version: '0'
104
105
  segments:
105
106
  - 0
106
- hash: 3975846568932287638
107
+ hash: 4118092927290813753
107
108
  requirements: []
108
109
  rubyforge_project:
109
110
  rubygems_version: 1.8.23