Class: Castaway::Size

Inherits:
Struct
  • Object
show all
Defined in:
lib/castaway/size.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#heightObject

Returns the value of attribute height

Returns:

  • (Object)

    the current value of height



3
4
5
# File 'lib/castaway/size.rb', line 3

def height
  @height
end

#widthObject

Returns the value of attribute width

Returns:

  • (Object)

    the current value of width



3
4
5
# File 'lib/castaway/size.rb', line 3

def width
  @width
end

Instance Method Details

#*(factor) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/castaway/size.rb', line 4

def *(factor)
  if factor.is_a?(Size)
    Size.new(width * factor.width, height * factor.height)
  else
    Size.new(width * factor, height * factor)
  end
end

#aspect_ratioObject



20
21
22
# File 'lib/castaway/size.rb', line 20

def aspect_ratio
  @aspect_ratio ||= width.to_f / height
end

#empty?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/castaway/size.rb', line 16

def empty?
  (width || 0).zero? && (height || 0).zero?
end

#present?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/castaway/size.rb', line 12

def present?
  width || height
end

#to_geometryObject



36
37
38
39
40
# File 'lib/castaway/size.rb', line 36

def to_geometry
  format('%.2fx%.2f', width, height).tap do |geometry|
    geometry << '!' if width && height
  end
end

#to_resolutionObject



42
43
44
# File 'lib/castaway/size.rb', line 42

def to_resolution
  format('%dx%d', width || 0, height || 0)
end

#to_sObject



32
33
34
# File 'lib/castaway/size.rb', line 32

def to_s
  format('(%.2f, %.2f)', width, height)
end

#with_height(height) ⇒ Object



24
25
26
# File 'lib/castaway/size.rb', line 24

def with_height(height)
  Size.new((aspect_ratio * height).to_i, height.to_i)
end

#with_width(width) ⇒ Object



28
29
30
# File 'lib/castaway/size.rb', line 28

def with_width(width)
  Size.new(width.to_i, (width / aspect_ratio).to_i)
end