Class: Akiva::Brain::Helpers::Units

Inherits:
Object
  • Object
show all
Defined in:
lib/akiva/core_brain/helpers/units.rb

Defined Under Namespace

Classes: CantCompareMoreThanTwoValuesError, IncompatibleUnitsError, InputMustBeAnArray, NeedTwoValuesToCompareError, TooLongToInstantiateError, UnitNotRecognizedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Units

Returns a new instance of Units.



14
15
16
# File 'lib/akiva/core_brain/helpers/units.rb', line 14

def initialize(input)
  @input = input
end

Instance Attribute Details

#first_valueObject (readonly)

Returns the value of attribute first_value.



12
13
14
# File 'lib/akiva/core_brain/helpers/units.rb', line 12

def first_value
  @first_value
end

#inputObject (readonly)

Returns the value of attribute input.



12
13
14
# File 'lib/akiva/core_brain/helpers/units.rb', line 12

def input
  @input
end

#instantiated_valuesObject (readonly)

Returns the value of attribute instantiated_values.



12
13
14
# File 'lib/akiva/core_brain/helpers/units.rb', line 12

def instantiated_values
  @instantiated_values
end

#last_valueObject (readonly)

Returns the value of attribute last_value.



12
13
14
# File 'lib/akiva/core_brain/helpers/units.rb', line 12

def last_value
  @last_value
end

#resultObject (readonly)

Returns the value of attribute result.



12
13
14
# File 'lib/akiva/core_brain/helpers/units.rb', line 12

def result
  @result
end

Class Method Details

.adjust_units(value_1, value_2) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/akiva/core_brain/helpers/units.rb', line 73

def self.adjust_units(value_1, value_2)
  if ((value_1.to_s.include?("e+") or value_1.to_s.include?("e-")) and (!value_2.to_s.include?("e+") and !value_2.to_s.include?("e-"))) or value_1.scalar.to_s.size > value_2.scalar.to_s.size
    value_1 = value_1.convert_to(value_2)
  elsif ((!value_1.to_s.include?("e+") and !value_1.to_s.include?("e-")) and (value_2.to_s.include?("e+") or value_2.to_s.include?("e-"))) or value_1.scalar.to_s.size < value_2.scalar.to_s.size
    value_2 = value_2.convert_to(value_1)
  end

  [value_1, value_2]
end

Instance Method Details

#compare(options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/akiva/core_brain/helpers/units.rb', line 18

def compare(options = {})
  check_presence_of_values
  check_type_of_values
  check_length_of_values_for_comparison
  instantiate_values
  check_compatibility_of_units

  @first_value = @instantiated_values.first
  @last_value = @instantiated_values.last

  if multiplicators = options[:multiplicators]
    @first_value = (@first_value * multiplicators[0]) if multiplicators[0]
    @last_value = (@last_value * multiplicators[1]) if multiplicators[1]
  end

  @first_value, @last_value = self.class.adjust_units(@first_value, @last_value)

  if @first_value > @last_value
    @result = "superior"
  elsif @first_value < @last_value
    @result = "inferior"
  else
    @result = "equal"
  end
end

#convert(new_unit = "") ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/akiva/core_brain/helpers/units.rb', line 54

def convert(new_unit = "")
  instantiate_values

  begin
    new_instantiated_value = @instantiated_value.convert_to(new_unit)
    @result = new_instantiated_value.scalar.to_f.to_s
    @result = @result[0..@result.size-3] if @result.match(/\.0$/) # 135.0 => 135
  rescue ArgumentError => error_string
    if error_string.to_s.include?("Unit not recognized") or error_string.to_s.include?("No Unit Specified")
      raise UnitNotRecognizedError
    elsif error_string.to_s.include?("Incompatible Units")
      raise IncompatibleUnitsError
    else
      raise
    end
  end
end

#sortObject



44
45
46
47
48
49
50
51
52
# File 'lib/akiva/core_brain/helpers/units.rb', line 44

def sort
  check_presence_of_values
  check_type_of_values
  check_maximum_length_of_values
  instantiate_values
  check_compatibility_of_units

  @result = @instantiated_values.sort
end