Module: Operable::Operations
- Included in:
- Operable
- Defined in:
- lib/operable/operations.rb
Instance Method Summary collapse
- #*(by) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(by) ⇒ Object
-
#add_or_subtract(method, other) ⇒ Object
Add or subtract one method from another.
-
#matches?(other) ⇒ Boolean
Compare equality between objects.
-
#multiply_or_divide(method, by) ⇒ Object
Multiply or divide values of this object by another.
Instance Method Details
#*(by) ⇒ Object
42 43 44 |
# File 'lib/operable/operations.rb', line 42 def *(by) multiply_or_divide(:*, by) end |
#+(other) ⇒ Object
23 24 25 |
# File 'lib/operable/operations.rb', line 23 def +(other) add_or_subtract(:+, other) end |
#-(other) ⇒ Object
27 28 29 |
# File 'lib/operable/operations.rb', line 27 def -(other) add_or_subtract(:-, other) end |
#/(by) ⇒ Object
46 47 48 |
# File 'lib/operable/operations.rb', line 46 def /(by) multiply_or_divide(:/, by) end |
#add_or_subtract(method, other) ⇒ Object
Add or subtract one method from another
method - name of method to perform, as a symbol ( :+ or :- ) other - other object to operate with
Returns the resulting object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/operable/operations.rb', line 10 def add_or_subtract(method, other) return self unless other self.class.new.tap do |o| operable_values.each do |k,v| our_value = v || 0 other_value = other.respond_to?(k) ? other.send(k) : 0 o.send("#{k}=", our_value.send(method, other_value || 0)) end end end |
#matches?(other) ⇒ Boolean
Compare equality between objects
Returns true or false
53 54 55 |
# File 'lib/operable/operations.rb', line 53 def matches?(other) operable_values == other.operable_values end |
#multiply_or_divide(method, by) ⇒ Object
Multiply or divide values of this object by another
Returns a new object
34 35 36 37 38 39 40 |
# File 'lib/operable/operations.rb', line 34 def multiply_or_divide(method, by) self.class.new.tap do |o| operable_values.each do |k,v| o.send("#{k}=", (v || 0).send(method, by)) end end end |