Class: HumanizeFraction::Humanizer

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#denominatorObject (readonly)

Returns the value of attribute denominator.



7
8
9
# File 'lib/humanize_fraction/humanizer.rb', line 7

def denominator
  @denominator
end

#integer_partObject (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

#numeratorObject (readonly)

Returns the value of attribute numerator.



7
8
9
# File 'lib/humanize_fraction/humanizer.rb', line 7

def numerator
  @numerator
end

#quarterObject (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

#shorthandObject (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, options = {})
  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, **options)
  elsif string_is_single_fraction?(string)
    numerator, denominator = string.split("/").map(&:to_i)
    new(numerator: numerator, denominator: denominator, **options)
  else
    raise ArgumentError, "Unable to extract fraction from string #{string}"
  end
end

Instance Method Details

#to_sObject



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