class Percentable::Percent

Private Class Methods

from_numeric(numeric) click to toggle source
# File lib/percentable/percent.rb, line 65
def self.from_numeric(numeric)
  case numeric
  when Numeric
    Percent.new(numeric*100)
  else
    fail TypeError, 'must inherit from Numeric'
  end
end
new(value) click to toggle source
# File lib/percentable/percent.rb, line 3
def initialize(value)
  if value.is_a? Percent
    @value = value.value
  else
    @value = value.to_f
  end
end

Private Instance Methods

*(other) click to toggle source
# File lib/percentable/percent.rb, line 39
def * other
  if other.is_a? Percent
    self.class.new(to_f * other.value)
  elsif other.respond_to? :coerce
    a, b = other.coerce(self)
    a * b
  else
    raise TypeError, "#{other.class} can't be coerced into Percent"
  end
end
<=>(other) click to toggle source
# File lib/percentable/percent.rb, line 35
def <=> other
  to_f <=> other.to_f
end
==(other) click to toggle source
# File lib/percentable/percent.rb, line 27
def == other
  (other.class == self.class && other.value == self.value) || other == self.to_f
end
coerce(other) click to toggle source
# File lib/percentable/percent.rb, line 74
def coerce other
  [CoercedPercent.new(self), other]
end
eql?(other) click to toggle source
# File lib/percentable/percent.rb, line 31
def eql? other
  self.send(:==, other)
end
to_f() click to toggle source
# File lib/percentable/percent.rb, line 19
def to_f
  value/100
end
to_i() click to toggle source
# File lib/percentable/percent.rb, line 23
def to_i
  value.to_i
end
to_percent() click to toggle source
# File lib/percentable/percent.rb, line 61
def to_percent
  self
end
to_s() click to toggle source
# File lib/percentable/percent.rb, line 15
def to_s
  '%g%%' % value
end
value() click to toggle source
# File lib/percentable/percent.rb, line 11
def value
  @value ||= 0.to_f
end