Class: HumanizeFraction::Humanizer
- Inherits:
-
Object
- Object
- HumanizeFraction::Humanizer
- Defined in:
- lib/humanize_fraction/humanizer.rb
Constant Summary collapse
- SINGLE_FRACTION =
/^\s*(\-?\d+)\/(\-?\d+)\s*$/- MIXED_FRACTION =
/^\s*(\-?\d*)\s+(\d+)\/(\d+)\s*$/
Instance Attribute Summary collapse
-
#denominator ⇒ Object
readonly
Returns the value of attribute denominator.
-
#integer_part ⇒ Object
readonly
Returns the value of attribute integer_part.
-
#numerator ⇒ Object
readonly
Returns the value of attribute numerator.
-
#quarter ⇒ Object
(also: #quarter?)
readonly
Returns the value of attribute quarter.
-
#shorthand ⇒ Object
(also: #shorthand?)
readonly
Returns the value of attribute shorthand.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(numerator:, denominator:, integer_part: nil, shorthand: false, quarter: false) ⇒ Humanizer
constructor
A new instance of Humanizer.
- #to_s ⇒ Object
Constructor Details
#initialize(numerator:, denominator:, integer_part: nil, shorthand: false, quarter: false) ⇒ Humanizer
Returns a new instance of Humanizer.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/humanize_fraction/humanizer.rb', line 9 def initialize(numerator:, denominator:, integer_part: nil, shorthand: false, quarter: false) [numerator, denominator].each do |number| if !number.is_a?(Integer) raise ArgumentError, "Expected Integers for numerator/denominator but got #{number.class.name}" end end if !integer_part.nil? && !integer_part.is_a?(Integer) raise ArgumentError, "Expected Integer or NilClass for integer_part but got #{integer_part.class.name}" end @integer_part = integer_part @numerator = numerator @denominator = denominator @shorthand = shorthand @quarter = quarter end |
Instance Attribute Details
#denominator ⇒ Object (readonly)
Returns the value of attribute denominator.
7 8 9 |
# File 'lib/humanize_fraction/humanizer.rb', line 7 def denominator @denominator end |
#integer_part ⇒ Object (readonly)
Returns the value of attribute integer_part.
7 8 9 |
# File 'lib/humanize_fraction/humanizer.rb', line 7 def integer_part @integer_part end |
#numerator ⇒ Object (readonly)
Returns the value of attribute numerator.
7 8 9 |
# File 'lib/humanize_fraction/humanizer.rb', line 7 def numerator @numerator end |
#quarter ⇒ Object (readonly) Also known as: quarter?
Returns the value of attribute quarter.
7 8 9 |
# File 'lib/humanize_fraction/humanizer.rb', line 7 def quarter @quarter end |
#shorthand ⇒ Object (readonly) Also known as: shorthand?
Returns the value of attribute shorthand.
7 8 9 |
# File 'lib/humanize_fraction/humanizer.rb', line 7 def shorthand @shorthand end |
Class Method Details
.from_string(string, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/humanize_fraction/humanizer.rb', line 35 def self.from_string(string, = {}) if !string.is_a?(String) raise ArgumentError, "Expected String but got #{string.class.name}" end if string_is_mixed_fraction?(string) whole, numerator, denominator = string.scan(MIXED_FRACTION).flatten.map(&:to_i) new(integer_part: whole, numerator: numerator, denominator: denominator, **) elsif string_is_single_fraction?(string) numerator, denominator = string.split("/").map(&:to_i) new(numerator: numerator, denominator: denominator, **) else raise ArgumentError, "Unable to extract fraction from string #{string}" end end |
Instance Method Details
#to_s ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/humanize_fraction/humanizer.rb', line 27 def to_s words = [] words << humanize_integer_part if !integer_part.nil? words << humanize_numerator words << humanize_denominator words.join(" ") end |