Class: Kiba::Extend::Data::ConvertibleFraction

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/kiba/extend/data/convertible_fraction.rb

Overview

Value object encoding an extracted string fraction (e.g. ‘1 1/2’) so it can be converted.

Can represent invalid/non-convertible “fractions”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fraction:, position:, whole: 0) ⇒ ConvertibleFraction

Returns a new instance of ConvertibleFraction.

Parameters:

  • whole (Integer) (defaults to: 0)

    whole number preceding a fraction

  • fraction (String)
  • position (Range)

    indicates position of fractional data within original string



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 19

def initialize(fraction:, position:, whole: 0)
  unless whole.is_a?(Integer)
    fail(TypeError,
      "`whole` must be an Integer")
  end
  unless position.is_a?(Range)
    fail(TypeError,
      "`position` must be a Range")
  end
  @whole = whole.freeze
  @fraction = fraction.freeze
  @position = position.freeze
end

Instance Attribute Details

#fractionObject (readonly)

Returns the value of attribute fraction.



13
14
15
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 13

def fraction
  @fraction
end

#positionObject (readonly)

Returns the value of attribute position.



13
14
15
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 13

def position
  @position
end

#wholeObject (readonly)

Returns the value of attribute whole.



13
14
15
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 13

def whole
  @whole
end

Instance Method Details

#<=>(other) ⇒ Object



75
76
77
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 75

def <=>(other)
  position.first <=> other.position.first
end

#==(other) ⇒ Object Also known as: eql?



68
69
70
71
72
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 68

def ==(other)
  whole == other.whole &&
    fraction == other.fraction &&
    position == other.position
end

#convertible?Boolean

Returns whether the fraction is indeed convertible.

Returns:

  • (Boolean)

    whether the fraction is indeed convertible



60
61
62
63
64
65
66
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 60

def convertible?
  Rational(fraction)
rescue ZeroDivisionError
  false
else
  true
end

#hashObject



79
80
81
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 79

def hash
  [self.class, whole, fraction, position].hash
end

#replace_in(val:, places: 4) ⇒ String

Parameters:

  • val (String)

    the value in which textual fraction will be replaced with a decimal

  • places (Integer) (defaults to: 4)

    maximum number of decimal places to keep in the resulting decimal value

Returns:

  • (String)


38
39
40
41
42
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 38

def replace_in(val:, places: 4)
  return val unless convertible?

  [prefix ? val[prefix] : "", to_s(places), val[suffix]].compact.join
end

#to_fFloat

Returns:

  • (Float)


45
46
47
48
49
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 45

def to_f
  return nil unless convertible?

  (Rational(fraction) + whole).to_f
end

#to_hObject



83
84
85
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 83

def to_h
  {whole: whole, fraction: fraction, position: position}
end

#to_s(places = 4) ⇒ String

Parameters:

  • places (Integer) (defaults to: 4)

Returns:

  • (String)


53
54
55
56
57
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 53

def to_s(places = 4)
  return nil unless convertible?

  (Rational(fraction) + whole).round(+places).to_f.to_s
end