Sha256: 585c539c767fae643bf4bda6fb018435c4e3b33df461ac42e81a0ada4e9fae95

Contents?: true

Size: 899 Bytes

Versions: 6

Compression:

Stored size: 899 Bytes

Contents

class CalculatorPage < UnderOs::Page

  def initialize
    @label = first('#result')

    find('button').each do |b|
      b.on(:tap){|e| handle_tap(e)}
    end
  end

  def handle_tap(event)
    case event.target.text
    when '0','1','2','3','4','5','6','7','8','9','.'
      @label.text  = '' if ['0', @first_value].include?(@label.text)
      @label.text += event.target.text if @label.text.size < 18
    when '÷','×','-','+'
      @first_value = @label.text
      @operator    = {'÷'=>'/','×'=>'*','-'=>'-','+'=>'+'}[event.target.text]
    when 'C'
      @first_value = nil
      @label.text  = '0'
    when '='
      calculate
    end
  end

  def calculate
    return if ! @first_value

    values = [@first_value, @label.text]
    values = values.any?{|v| v.include?('.')} ? values.map(&:to_f) : values.map(&:to_i)

    @label.text = values[0].send(@operator, values[1]).to_s
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
under-os-1.4.0 app/pages/calculator_page.rb
under-os-1.3.0 app/pages/calculator_page.rb
under-os-1.2.1 app/pages/calculator_page.rb
under-os-1.2.0 app/pages/calculator_page.rb
under-os-1.1.0 app/pages/calculator_page.rb
under-os-1.0.0 app/pages/calculator_page.rb