Class: Amazon::Associates::Measurement

Inherits:
ApiResult show all
Includes:
Comparable
Defined in:
lib/amazon-associates/types/measurement.rb

Instance Method Summary collapse

Constructor Details

#initialize(value = nil, units = 'pixels') ⇒ Measurement

Returns a new instance of Measurement.



9
10
11
12
13
# File 'lib/amazon-associates/types/measurement.rb', line 9

def initialize(value = nil, units = 'pixels')
  @value = value && Float(value)
  @units = units.to_s
  normalize_hundredths
end

Instance Method Details

#<=>(other) ⇒ Object



27
28
29
30
31
# File 'lib/amazon-associates/types/measurement.rb', line 27

def <=>(other)
  return nil unless @units == other.units

  @value <=> other.value
end

#to_iObject



23
24
25
# File 'lib/amazon-associates/types/measurement.rb', line 23

def to_i
  @value.round
end

#to_sObject Also known as: inspect



15
16
17
18
19
20
# File 'lib/amazon-associates/types/measurement.rb', line 15

def to_s
  value = @value.whole? ? @value.to_i : @value
  #singularize here to avoid comparison problems
  units = @value == 1 ? @units.singularize : @units
  [value, units].join(' ')
end