Sha256: 6bbc78206376f52599f45f06868a1ac021e79e3d326ce7170a7148f46cdedc3e

Contents?: true

Size: 856 Bytes

Versions: 118

Compression:

Stored size: 856 Bytes

Contents

class Calculator
  module Model
    class Command
      class Operation < Command
        def execute
          if number1 && number2 && operation && !last_command.is_a?(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

118 entries across 118 versions & 1 rubygems

Version Path
glimmer-dsl-swt-4.30.0.0 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.29.0.0 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.28.0.0 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.27.0.1 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.27.0.0 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.26.0.1 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.26.0.0 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.25.0.2 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.25.0.1 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.25.0.0 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.24.4.8 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.24.4.7 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.24.4.6 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.24.4.5 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.24.4.4 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.24.4.3 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.24.4.2 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.24.4.1 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.24.4.0 samples/elaborate/calculator/model/command/operation.rb
glimmer-dsl-swt-4.24.3.2 samples/elaborate/calculator/model/command/operation.rb