Sha256: 67349e17b2de7c152de1e0273018f7ca7395b01fa08010f1f73c3daba46157ec

Contents?: true

Size: 874 Bytes

Versions: 3

Compression:

Stored size: 874 Bytes

Contents

module Glimmer
  class Calculator
    class Command
      class Operation < Command
        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
        
        def calculate
          calculation_result = BigDecimal.new(number1.to_s).send(operation_method, BigDecimal.new(number2.to_s)).to_f
          calculation_result.to_s.match(/\.0+$/) ? calculation_result.to_i : calculation_result
        end
        
        # Subclasses must implement to indicate operation method on number (e.g. :+ for addition)
        def operation_method
          raise 'Not implemented!'
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
glimmer-cs-calculator-1.1.3 lib/models/glimmer/calculator/command/operation.rb
glimmer-cs-calculator-1.1.2 lib/models/glimmer/calculator/command/operation.rb
glimmer-cs-calculator-1.1.1 lib/models/glimmer/calculator/command/operation.rb