Class: TrustyCms::Geometry
- Inherits:
-
Object
- Object
- TrustyCms::Geometry
- Defined in:
- lib/trusty_cms/geometry.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#modifier ⇒ Object
readonly
Returns the value of attribute modifier.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #=~(other) ⇒ Object
- #aspect ⇒ Object
- #horizontal? ⇒ Boolean
-
#initialize(width, height = nil, modifier = nil) ⇒ Geometry
constructor
A new instance of Geometry.
- #scaled_by(other) ⇒ Object
- #scaled_to_fit(other) ⇒ Object
- #square? ⇒ Boolean
- #to_s ⇒ Object
- #transformed_by(other) ⇒ Object (also: #*)
- #vertical? ⇒ Boolean
- #without_modifier ⇒ Object
Constructor Details
#initialize(width, height = nil, modifier = nil) ⇒ Geometry
Returns a new instance of Geometry.
20 21 22 23 24 |
# File 'lib/trusty_cms/geometry.rb', line 20 def initialize(width, height = nil, modifier = nil) @width = width.to_i @height = height.to_i @modifier = modifier end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
6 7 8 |
# File 'lib/trusty_cms/geometry.rb', line 6 def height @height end |
#modifier ⇒ Object (readonly)
Returns the value of attribute modifier.
6 7 8 |
# File 'lib/trusty_cms/geometry.rb', line 6 def modifier @modifier end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
6 7 8 |
# File 'lib/trusty_cms/geometry.rb', line 6 def width @width end |
Class Method Details
.parse(value) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/trusty_cms/geometry.rb', line 8 def self.parse(value) return value if value.is_a?(Geometry) match = value.to_s.strip.match(/\A(\d*)x(\d*)([%<>#^!@])?\z/) raise StyleError, "Unrecognized geometry: #{value.inspect}" unless match width = match[1].to_s.empty? ? 0 : match[1].to_i height = match[2].to_s.empty? ? 0 : match[2].to_i modifier = match[3] new(width, height, modifier) end |
Instance Method Details
#==(other) ⇒ Object
51 52 53 |
# File 'lib/trusty_cms/geometry.rb', line 51 def ==(other) to_s == other.to_s end |
#=~(other) ⇒ Object
55 56 57 |
# File 'lib/trusty_cms/geometry.rb', line 55 def =~(other) height.to_i == other.height.to_i && width.to_i == other.width.to_i end |
#aspect ⇒ Object
94 95 96 97 98 |
# File 'lib/trusty_cms/geometry.rb', line 94 def aspect return nil if width.to_f == 0.0 || height.to_f == 0.0 width.to_f / height.to_f end |
#horizontal? ⇒ Boolean
108 109 110 |
# File 'lib/trusty_cms/geometry.rb', line 108 def horizontal? width.to_i > height.to_i end |
#scaled_by(other) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/trusty_cms/geometry.rb', line 79 def scaled_by(other) if other.is_a?(Numeric) scaled_width = (width * other.fdiv(100)).round scaled_height = (height * other.fdiv(100)).round return Geometry.new(scaled_width, scaled_height) end other = Geometry.new("#{other}%") unless other.is_a?(Geometry) if other.height.positive? Geometry.new(width * other.width / 100, height * other.height / 100) else Geometry.new(width * other.width / 100, height * other.width / 100) end end |
#scaled_to_fit(other) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/trusty_cms/geometry.rb', line 59 def scaled_to_fit(other) if other.width.positive? && other.height.zero? Geometry.new(other.width, height * other.width / width) elsif other.width.zero? && other.height.positive? Geometry.new(width * other.height / height, other.height) else product_width = other.width * height product_height = other.height * width if product_width == product_height other.without_modifier elsif product_width > product_height scaled_width = (width * other.height.fdiv(height)).round Geometry.new(scaled_width, other.height) else scaled_height = (height * other.width.fdiv(width)).round Geometry.new(other.width, scaled_height) end end end |
#square? ⇒ Boolean
100 101 102 |
# File 'lib/trusty_cms/geometry.rb', line 100 def square? width.to_i == height.to_i end |
#to_s ⇒ Object
112 113 114 115 |
# File 'lib/trusty_cms/geometry.rb', line 112 def to_s suffix = modifier.to_s "#{width}x#{height}#{suffix}" end |
#transformed_by(other) ⇒ Object Also known as: *
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/trusty_cms/geometry.rb', line 30 def transformed_by(other) other = Geometry.parse(other) return other.without_modifier if self =~ other || ['#', '!', '^'].include?(other.modifier) raise TransformationError, 'geometry is not transformable without both width and height' if height.zero? || width.zero? case other.modifier when '>' (other.width < width || other.height < height) ? scaled_to_fit(other) : self when '<' (other.width > width && other.height > height) ? scaled_to_fit(other) : self when '%' scaled_by(other) when '@' scaled_by((other.width * 100).fdiv(width * height)) else scaled_to_fit(other) end end |
#vertical? ⇒ Boolean
104 105 106 |
# File 'lib/trusty_cms/geometry.rb', line 104 def vertical? height.to_i > width.to_i end |