Module: Metro::Units::CalculationValidations

Included in:
Dimensions, Point, RectangleBounds, Scale
Defined in:
lib/metro/units/calculation_validations.rb

Instance Method Summary collapse

Instance Method Details

#*(value) ⇒ Object

Multiply this object and another object.

Returns:

  • a new object that is the product of the two objects.



48
49
50
# File 'lib/metro/units/calculation_validations.rb', line 48

def *(value)
  self.class.new *calculate(value,:*)
end

#+(value) ⇒ Object

Add this object to another object.

Returns:

  • a new object that is the sum of the two objects



29
30
31
# File 'lib/metro/units/calculation_validations.rb', line 29

def +(value)
  self.class.new *calculate(value,:+)
end

#-(value) ⇒ Object

Subtract the other object from this object.

Returns:

  • a new object that is the difference of the original object and the value specified.



39
40
41
# File 'lib/metro/units/calculation_validations.rb', line 39

def -(value)
  self.class.new *calculate(value,:-)
end

#calculate(value, operation) ⇒ Array

This generic method will perform the calculation defined by the operation for all the calculation requirements defined.

Parameters:

  • value (value)

    this is the other value that is being added, subtracted, etc. to the current object.

  • operation (Symbol)

    this is the mathematical operation that is being performed between all the calc requirements of the current object and other value.

Returns:

  • (Array)

    an array of reults from the calculations of all the requirements.



65
66
67
68
69
70
# File 'lib/metro/units/calculation_validations.rb', line 65

def calculate(value,operation)
  check_calculation_requirements(value)
  calculation_requirements.map do |requirement|
    send(requirement).send(operation,value.send(requirement))
  end
end

#calculation_requirementsArray

Note:

this method is intended to be defined in the including class. This method is included here when one has not been provided.

Returns an array of methods that are required to be on the object for it to be correctly calculated.

Returns:

  • (Array)

    an array of methods that are required to be on the object for it to be correctly calculated.



20
21
22
# File 'lib/metro/units/calculation_validations.rb', line 20

def calculation_requirements
  []
end

#check_calculation_requirements(value) ⇒ Object

Parameters:

  • value (Object)

    the other object that needs to be validated.



8
9
10
11
12
# File 'lib/metro/units/calculation_validations.rb', line 8

def check_calculation_requirements(value)
  if calculation_requirements.find { |method| ! value.respond_to?(method) }
    raise "Unable to perform operation with #{value} #{value.class} It is missing a property #{calculation_requirements.join(",")}"
  end
end