/
dimension.rb
64 lines (54 loc) · 1.5 KB
/
dimension.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module Qcontent
class Dimension
class InvalidDimension < ::RuntimeError; end;
include Comparable
attr_accessor :name, :width, :height
def initialize(*args)
first = args.shift
case first
when Array
self.width, self.height = first
when Hash
self.width = first['width'] || first[:width]
self.height = first['height'] || first[:height]
self.name = first['name'] || first[:name]
when Qcontent::Dimension
self.name, self.width, self.height = first.name, first.width, first.height
when Fixnum
self.width = first
self.height = args.shift
else
if first.to_i == 0 # its a name
self.name = first
d = Qcontent::Dimension.new(*args)
self.width, self.height = d.width, d.height
else
self.width, self.height = (first.is_a?(String) ? first.split('x') : first)
self.height ||= args.shift
end
end
@name ||= dimension_s
end
def ==(other)
self.width == other.width && self.height == other.height
end
def width=(w)
@width = w.nil? ? nil : w.to_i
end
def height=(h)
@height = h.nil? ? nil : h.to_i
end
def to_s(join = 'x')
name ? name : dimension_s(join)
end
def to_a
[width, height]
end
def dimension_s(join = 'x')
"#{width}#{join}#{height}"
end
def inspect
"<Dimension: #{name}, #{dimension_s}>"
end
end
end