Class: RRTF::Page::Margin
- Inherits:
-
Object
- Object
- RRTF::Page::Margin
- Defined in:
- lib/rrtf/page/margin.rb
Overview
Represents the left, right, top, and bottom margin in a document page.
Instance Attribute Summary collapse
Class Method Summary collapse
-
.from_string(string) ⇒ Margin
Extracts a margin object from a string.
-
.parse_string(string) ⇒ Hash<String, Integer>
Extracts a margin hash from a string.
Instance Method Summary collapse
-
#==(obj) ⇒ Object
(also: #eql?)
initialize.
-
#initialize(value = nil) ⇒ Margin
constructor
Builds a new margin object from a string or hash.
Constructor Details
#initialize(value = nil) ⇒ Margin
Note:
Margins are stored internally in twentieth points (twips).
Builds a new margin object from a string or hash.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/rrtf/page/margin.rb', line 62 def initialize(value = nil) = { # default 1 inch margins "left" => 1440, "right" => 1440, "top" => 1440, "bottom" => 1440 } case value when String = .merge(self.class.parse_string(value)) when Hash = .merge(value) when nil else RRTF::RTFError.fire("Cannot create margin from '#{value}'.") end # case @left = .delete("left") @right = .delete("right") @top = .delete("top") @bottom = .delete("bottom") end |
Instance Attribute Details
#bottom ⇒ Object
6 7 8 |
# File 'lib/rrtf/page/margin.rb', line 6 def bottom @bottom end |
#left ⇒ Object
6 7 8 |
# File 'lib/rrtf/page/margin.rb', line 6 def left @left end |
#right ⇒ Object
6 7 8 |
# File 'lib/rrtf/page/margin.rb', line 6 def right @right end |
#top ⇒ Object
6 7 8 |
# File 'lib/rrtf/page/margin.rb', line 6 def top @top end |
Class Method Details
.from_string(string) ⇒ Margin
Extracts a margin object from a string.
16 17 18 |
# File 'lib/rrtf/page/margin.rb', line 16 def self.from_string(string) self.new(parse_string(string)) end |
.parse_string(string) ⇒ Hash<String, Integer>
Extracts a margin hash from a string.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rrtf/page/margin.rb', line 30 def self.parse_string(string) values = string.split(',').map(&:strip).collect{ |str| RRTF::Utilities.value2twips(str) } case values.length when 1 tblr = values.first {"top" => tblr, "bottom" => tblr, "left" => tblr, "right" => tblr} when 2 tb = values.last lr = values.first {"top" => tb, "bottom" => tb, "left" => lr, "right" => lr} when 4 l = values[0] r = values[1] t = values[2] b = values[3] {"top" => t, "bottom" => b, "left" => l, "right" => r} else RRTF::RTFError.fire("Invalid margin '#{string}'.") end # case end |
Instance Method Details
#==(obj) ⇒ Object Also known as: eql?
initialize
87 88 89 |
# File 'lib/rrtf/page/margin.rb', line 87 def ==(obj) obj.class == self.class && obj.state == state end |