Module: Metro::Units::CalculationValidations
- Included in:
- Dimensions, Point, RectangleBounds, Scale
- Defined in:
- lib/metro/units/calculation_validations.rb
Instance Method Summary collapse
-
#*(value) ⇒ Object
Multiply this object and another object.
-
#+(value) ⇒ Object
Add this object to another object.
-
#-(value) ⇒ Object
Subtract the other object from this object.
-
#calculate(value, operation) ⇒ Array
This generic method will perform the calculation defined by the operation for all the calculation requirements defined.
-
#calculation_requirements ⇒ Array
An array of methods that are required to be on the object for it to be correctly calculated.
- #check_calculation_requirements(value) ⇒ Object
Instance Method Details
#*(value) ⇒ Object
Multiply this object and another object.
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.
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.
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.
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_requirements ⇒ Array
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.
20 21 22 |
# File 'lib/metro/units/calculation_validations.rb', line 20 def calculation_requirements [] end |
#check_calculation_requirements(value) ⇒ Object
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 |