Class: Kiba::Extend::Data::ConvertibleFraction
- Inherits:
-
Object
- Object
- Kiba::Extend::Data::ConvertibleFraction
- 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
-
#fraction ⇒ Object
readonly
Returns the value of attribute fraction.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
-
#whole ⇒ Object
readonly
Returns the value of attribute whole.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
-
#==(other) ⇒ Object
(also: #eql?)
-
#convertible? ⇒ Boolean
Whether the fraction is indeed convertible.
-
#hash ⇒ Object
-
#initialize(fraction:, position:, whole: 0) ⇒ ConvertibleFraction
constructor
A new instance of ConvertibleFraction.
-
#replace_in(val:, places: 4) ⇒ String
-
#to_f ⇒ Float
-
#to_h ⇒ Object
-
#to_s(places = 4) ⇒ String
Constructor Details
#initialize(fraction:, position:, whole: 0) ⇒ ConvertibleFraction
Returns a new instance of ConvertibleFraction.
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
#fraction ⇒ Object (readonly)
Returns the value of attribute fraction.
13 14 15 |
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 13 def fraction @fraction end |
#position ⇒ Object (readonly)
Returns the value of attribute position.
13 14 15 |
# File 'lib/kiba/extend/data/convertible_fraction.rb', line 13 def position @position end |
#whole ⇒ Object (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.
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 |
#hash ⇒ Object
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
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_f ⇒ 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_h ⇒ Object
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
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 |