Class: Percentage

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/percentage.rb,
lib/percentage/yaml.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Percentage

Returns a new instance of Percentage.



8
9
10
11
12
# File 'lib/percentage.rb', line 8

def initialize(value)
  @value = value

  freeze
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/percentage.rb', line 4

def value
  @value
end

Class Method Details

.change(a, b) ⇒ Object



150
151
152
# File 'lib/percentage.rb', line 150

def Percentage.change(a, b)
  Percentage.new((b - a).to_r / a) unless a.zero?
end

Instance Method Details

#*(object) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/percentage.rb', line 77

def *(object)
  case object
  when self.class
    self.class.new(fractional_value.to_r * object.fractional_value)
  else
    fractional_value.coerce(object).inject(&:*)
  end
end

#+(object) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/percentage.rb', line 65

def +(object)
  if self.class === object
    if @value.integer? ^ object.value.integer?
      self.class.new(fractional_value + object.fractional_value)
    else
      self.class.new(@value + object.value)
    end
  else
    raise TypeError, "cannot add #{object.class} to #{self.class}"
  end
end

#-@Object



111
112
113
# File 'lib/percentage.rb', line 111

def -@
  self.class.new(-@value)
end

#<=>(object) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/percentage.rb', line 49

def <=>(object)
  if self.class === object
    fractional_value <=> object.fractional_value
  elsif Numeric === object
    fractional_value <=> object
  end
end

#coerce(other) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/percentage.rb', line 86

def coerce(other)
  case other
  when Numeric
    return fractional_value, other
  else
    raise TypeError, "#{self.class} can't be coerced into #{other.class}"
  end
end

#encode_with(coder) ⇒ Object



4
5
6
# File 'lib/percentage/yaml.rb', line 4

def encode_with(coder)
  coder.represent_scalar(nil, to_s)
end

#eql?(object) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/percentage.rb', line 57

def eql?(object)
  object.instance_of?(self.class) && @value.eql?(object.value)
end

#hashObject



61
62
63
# File 'lib/percentage.rb', line 61

def hash
  @value.hash
end

#inspectObject



123
124
125
# File 'lib/percentage.rb', line 123

def inspect
  "<Percentage: value=#{value.inspect}>"
end

#negative?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/percentage.rb', line 119

def negative?
  @value.negative?
end

#nonzero?Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/percentage.rb', line 44

def nonzero?
  return if @value.zero?
  self
end

#positive?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/percentage.rb', line 115

def positive?
  @value.positive?
end

#scale(n) ⇒ Object



107
108
109
# File 'lib/percentage.rb', line 107

def scale(n)
  self.class.new(@value * n)
end

#to_dObject



30
31
32
33
34
35
36
37
38
# File 'lib/percentage.rb', line 30

def to_d
  if @value.integer?
    @value.to_d / 100
  elsif BigDecimal === @value
    @value
  else
    @value.to_d(0)
  end
end

#to_fObject



18
19
20
# File 'lib/percentage.rb', line 18

def to_f
  (fractional_value * 100).to_f
end

#to_iObject



14
15
16
# File 'lib/percentage.rb', line 14

def to_i
  (fractional_value * 100).to_i
end

#to_rObject



26
27
28
# File 'lib/percentage.rb', line 26

def to_r
  fractional_value.to_r
end

#to_sObject



22
23
24
# File 'lib/percentage.rb', line 22

def to_s
  "#{string_value}%"
end

#truncate(ndigits = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/percentage.rb', line 95

def truncate(ndigits = nil)
  return self if @value.integer?

  value = @value * 100

  if ndigits.nil?
    self.class.new(value.truncate)
  else
    self.class.new(value.truncate(ndigits) / 100)
  end
end

#zero?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/percentage.rb', line 40

def zero?
  @value.zero?
end