Class: Percentage

Inherits:
Numeric
  • Object
show all
Defined in:
lib/percentage.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val = 0, options = {}) ⇒ Percentage

Returns a new instance of Percentage.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/percentage.rb', line 5

def initialize(val = 0, options = {})
  val = 0.0            if !val
  val = 1.0            if val == true
  val = val.to_r       if val.is_a?(String) && val['/']
  val = val.to_f       if val.is_a?(Complex) || val.is_a?(Rational)
  val = val.to_i       if val.is_a?(String) && !val['.']
  val = val.to_d / 100 if val.is_a?(Integer) || (val.is_a?(String) && val['%'])
  val = val.value      if val.is_a? self.class

  @value = val.to_d
end

Class Method Details

.from_amount(val = 0, options = {}) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/percentage.rb', line 120

def self.from_amount(val = 0, options = {})
  val = val.to_r  if val.is_a?(String) && val['/']
  val = val.to_d  if val.is_a?(String)
  val = val / 100 if val.is_a?(Numeric) && !val.integer?

  self.new val, options
end

.from_fraction(val = 0, options = {}) ⇒ Object

Additional initialization methods



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

def self.from_fraction(val = 0, options = {})
  val = val.to_i if val.is_a?(String) && !(val['/'] || val['%'] || val['.'])
  val = val.to_d if val.is_a?(Integer)

  self.new val, options
end

Instance Method Details

#<=>(other) ⇒ Object



91
92
93
# File 'lib/percentage.rb', line 91

def <=> other
  self.to_f <=> other.to_f
end

#==(other) ⇒ Object

Comparisons



83
84
85
# File 'lib/percentage.rb', line 83

def == other
  self.eql?(other) || self.to_f == other
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/percentage.rb', line 87

def eql? other
  self.class == other.class && self.value == other.value
end

#format(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/percentage.rb', line 48

def format(options = {})
  # set defaults; all other options default to false
  options[:percent_sign] = options.fetch :percent_sign, true

  if options[:as_decimal]
    return self.to_str
  elsif options[:rounded]
    string = self.to_float.round.to_s
  elsif options[:no_decimal]
    string = self.to_i.to_s
  elsif options[:no_decimal_if_whole]
    string = self.to_s
  else
    string = self.to_float.to_s
  end

  string += ' ' if options[:space_before_sign]
  string += '%' if options[:percent_sign]
  return string
end

#inspectObject



76
77
78
# File 'lib/percentage.rb', line 76

def inspect
  "#<#{self.class.name}:#{self.object_id}, #{self.value.inspect}>"
end

#to_amountObject

Additional conversion methods



72
73
74
# File 'lib/percentage.rb', line 72

def to_amount
  (int = self.to_i) == (float = self.to_float) ? int : float
end

#to_cObject



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

def to_c; self.value.to_c; end

#to_complexObject

Convert percentage fraction to percent amount



36
# File 'lib/percentage.rb', line 36

def to_complex;  (self.value * 100).to_c; end

#to_dObject



29
# File 'lib/percentage.rb', line 29

def to_d; self.value.to_d; end

#to_decimalObject



37
# File 'lib/percentage.rb', line 37

def to_decimal;  (self.value * 100).to_d; end

#to_fObject



30
# File 'lib/percentage.rb', line 30

def to_f; self.value.to_f; end

#to_floatObject



38
# File 'lib/percentage.rb', line 38

def to_float;    (self.value * 100).to_f; end

#to_iObject

Convert percentage to different number formats



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

def to_i; (self.value * 100).to_i; end

#to_rObject



31
# File 'lib/percentage.rb', line 31

def to_r; self.value.to_r; end

#to_rationalObject



39
# File 'lib/percentage.rb', line 39

def to_rational; (self.value * 100).to_r; end

#to_sObject

String conversion methods



44
# File 'lib/percentage.rb', line 44

def to_s;      self.to_amount.to_s; end

#to_strObject



45
# File 'lib/percentage.rb', line 45

def to_str;    self.to_f.to_s;      end

#to_stringObject



46
# File 'lib/percentage.rb', line 46

def to_string; self.to_s + '%';     end

#valueObject

Attributes



20
21
22
# File 'lib/percentage.rb', line 20

def value
  @value ||= 0
end