Class: Skr::Core::Numbers::PercNum
- Inherits:
-
Object
- Object
- Skr::Core::Numbers::PercNum
- Defined in:
- lib/skr/core/numbers.rb
Overview
### PercNum
A “percnum” is a Stockor invention *(or abomination, depending on your POV)*. It’s a string that contains a number and an optional percent sign. If the percent sign is present, the number is treated as a percentage. If desired the user may also input negative numbers which will invert the sense of the method.
It’s intended to be a user-friendly method to provide one input box for “discount” or “surcharge”, and allow the user to input either a flat amount such as 4.50, or a percentage like 20%
Instance Method Summary collapse
-
#credit_to(amount) ⇒ BigDecimal
Adds the PercNum to the specified amount.
-
#debit_from(amount) ⇒ BigDecimal
Subtracts the PercNum to the specified amount.
-
#initialize(perc_or_num) ⇒ PercNum
constructor
A new instance of PercNum.
-
#is_percentage? ⇒ Boolean
Is the PercNum percentage based or absolute.
-
#present? ⇒ Boolean
If PercNum was initialized with a blank string.
Constructor Details
#initialize(perc_or_num) ⇒ PercNum
Returns a new instance of PercNum.
31 32 33 34 35 36 37 |
# File 'lib/skr/core/numbers.rb', line 31 def initialize( perc_or_num ) @is_perc = !! perc_or_num.to_s.match( /\%\s*$/ ) @right_side = BigDecimal.new( perc_or_num, 5 ) if is_percentage? @right_side *= 0.01 end end |
Instance Method Details
#credit_to(amount) ⇒ BigDecimal
Adds the PercNum to the specified amount.
45 46 47 |
# File 'lib/skr/core/numbers.rb', line 45 def credit_to( amount ) is_percentage? ? ( 1 + @right_side ) * amount : amount += @right_side end |
#debit_from(amount) ⇒ BigDecimal
Subtracts the PercNum to the specified amount.
55 56 57 |
# File 'lib/skr/core/numbers.rb', line 55 def debit_from( amount ) is_percentage? ? ( 1 - @right_side ) * amount : amount -= @right_side end |
#is_percentage? ⇒ Boolean
Is the PercNum percentage based or absolute
65 66 67 |
# File 'lib/skr/core/numbers.rb', line 65 def is_percentage? @is_perc end |
#present? ⇒ Boolean
If PercNum was initialized with a blank string
60 61 62 |
# File 'lib/skr/core/numbers.rb', line 60 def present? ! @right_side.zero? end |