Class: Metro::Units::Dimensions

Inherits:
Struct
  • Object
show all
Includes:
CalculationValidations
Defined in:
lib/metro/units/dimensions.rb

Overview

Represents an object that contains both the width and height.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CalculationValidations

#*, #+, #-, #calculate, #check_calculation_requirements

Instance Attribute Details

#heightObject

Returns the value of attribute height

Returns:

  • (Object)

    the current value of height



7
8
9
# File 'lib/metro/units/dimensions.rb', line 7

def height
  @height
end

#widthObject

Returns the value of attribute width

Returns:

  • (Object)

    the current value of width



7
8
9
# File 'lib/metro/units/dimensions.rb', line 7

def width
  @width
end

Class Method Details

.noneObject

Create a dimensions objects with zero width and zero height.



13
14
15
# File 'lib/metro/units/dimensions.rb', line 13

def self.none
  of 0.0, 0.0
end

.of(width = 0.0, height = 0.0) ⇒ Object

An alternate way of creating a dimensions object which will enforce that the values added are converted to floating point numbers.



32
33
34
# File 'lib/metro/units/dimensions.rb', line 32

def self.of(width=0.0,height=0.0)
  new width.to_f, height.to_f
end

.parse(string) ⇒ Object

Parse a string representation of a dimensions object. The supported formated is a comma-delimited string that contains two attributes width and height.



22
23
24
# File 'lib/metro/units/dimensions.rb', line 22

def self.parse(string)
  of *string.to_s.split(",",2).map(&:to_f)
end

Instance Method Details

#<=>(value) ⇒ Fixnum

Compare the dimension to another dimensions-like structure.

Returns:

  • (Fixnum)

    -1 if the dimensions is smaller than the other dimension, 0 if the dimensions are exactly the same, 1 if the dimensions are bigger then the other dimensions.



47
48
49
50
# File 'lib/metro/units/dimensions.rb', line 47

def <=>(value)
  check_calculation_requirements(value)
  (width * height) <=> (value.width * value.height)
end

#to_sObject



36
37
38
# File 'lib/metro/units/dimensions.rb', line 36

def to_s
  "#{width},#{height}"
end