Class: Percentage
- Inherits:
-
Object
- Object
- Percentage
- Includes:
- Comparable
- Defined in:
- lib/percentage.rb,
lib/percentage/yaml.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #*(object) ⇒ Object
- #+(object) ⇒ Object
- #-@ ⇒ Object
- #<=>(object) ⇒ Object
- #coerce(other) ⇒ Object
- #encode_with(coder) ⇒ Object
- #eql?(object) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(value) ⇒ Percentage
constructor
A new instance of Percentage.
- #negative? ⇒ Boolean
- #positive? ⇒ Boolean
- #scale(n) ⇒ Object
- #to_d ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_r ⇒ Object
- #to_s ⇒ Object
- #truncate(ndigits = nil) ⇒ Object
- #zero? ⇒ Boolean
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
#value ⇒ Object (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
141 142 143 |
# File 'lib/percentage.rb', line 141 def Percentage.change(a, b) Percentage.new((b - a).to_r / a) unless a.zero? end |
Instance Method Details
#*(object) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/percentage.rb', line 72 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
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/percentage.rb', line 60 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
106 107 108 |
# File 'lib/percentage.rb', line 106 def -@ self.class.new(-@value) end |
#<=>(object) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/percentage.rb', line 44 def <=>(object) if self.class === object fractional_value <=> object.fractional_value elsif Numeric === object fractional_value <=> object end end |
#coerce(other) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/percentage.rb', line 81 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
52 53 54 |
# File 'lib/percentage.rb', line 52 def eql?(object) object.instance_of?(self.class) && @value.eql?(object.value) end |
#hash ⇒ Object
56 57 58 |
# File 'lib/percentage.rb', line 56 def hash @value.hash end |
#negative? ⇒ Boolean
114 115 116 |
# File 'lib/percentage.rb', line 114 def negative? @value.negative? end |
#positive? ⇒ Boolean
110 111 112 |
# File 'lib/percentage.rb', line 110 def positive? @value.positive? end |
#scale(n) ⇒ Object
102 103 104 |
# File 'lib/percentage.rb', line 102 def scale(n) self.class.new(@value * n) end |
#to_d ⇒ Object
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_f ⇒ Object
18 19 20 |
# File 'lib/percentage.rb', line 18 def to_f (fractional_value * 100).to_f end |
#to_i ⇒ Object
14 15 16 |
# File 'lib/percentage.rb', line 14 def to_i (fractional_value * 100).to_i end |
#to_r ⇒ Object
26 27 28 |
# File 'lib/percentage.rb', line 26 def to_r fractional_value.to_r end |
#to_s ⇒ Object
22 23 24 |
# File 'lib/percentage.rb', line 22 def to_s "#{string_value}%" end |
#truncate(ndigits = nil) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/percentage.rb', line 90 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
40 41 42 |
# File 'lib/percentage.rb', line 40 def zero? @value.zero? end |