Class: Glimmer::Calculator::Command::Operation
- Inherits:
-
Glimmer::Calculator::Command
- Object
- Glimmer::Calculator::Command
- Glimmer::Calculator::Command::Operation
- Defined in:
- lib/models/glimmer/calculator/command/operation.rb,
lib/models/glimmer/calculator/command/operation/add.rb,
lib/models/glimmer/calculator/command/operation/divide.rb,
lib/models/glimmer/calculator/command/operation/multiply.rb,
lib/models/glimmer/calculator/command/operation/subtract.rb
Defined Under Namespace
Classes: Add, Divide, Multiply, Subtract
Instance Attribute Summary
Attributes inherited from Glimmer::Calculator::Command
Instance Method Summary collapse
- #calculate ⇒ Object
- #execute ⇒ Object
-
#operation_method ⇒ Object
Subclasses must implement to indicate operation method on number (e.g. :+ for addition).
Methods inherited from Glimmer::Calculator::Command
#command_history, command_history, for, #initialize, keyword, keyword_to_command_class_mapping, keywords, #last_command, #last_result, #number1, #number1=, #number2, #number2=, #operation, #operation=
Constructor Details
This class inherits a constructor from Glimmer::Calculator::Command
Instance Method Details
#calculate ⇒ Object
15 16 17 18 |
# File 'lib/models/glimmer/calculator/command/operation.rb', line 15 def calculate calculation_result = BigDecimal.new(number1.to_s).send(operation_method, BigDecimal.new(number2.to_s)).to_s('F') calculation_result.match(/\.0+$/) ? calculation_result.to_i : calculation_result end |
#execute ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/models/glimmer/calculator/command/operation.rb', line 5 def execute if number1 && number2 && operation && !last_command.equals? self.result = operation.calculate.to_s self.number1 = self.result else self.result = last_result || '0' self.operation = self end end |
#operation_method ⇒ Object
Subclasses must implement to indicate operation method on number (e.g. :+ for addition)
21 22 23 |
# File 'lib/models/glimmer/calculator/command/operation.rb', line 21 def operation_method raise 'Not implemented!' end |