Sha256: c60ad13c0e0d48b4f7947368bb805db5711e049c5f588d3c1d6544889075a2c6

Contents?: true

Size: 779 Bytes

Versions: 1

Compression:

Stored size: 779 Bytes

Contents

module Omnibar
  class Calculate < Query
    include Math

    def result
      if value.is_a?(Integer)
        [sanitized_input, value].join(' = ')
      else
        value.to_s
      end
    end

    def value
      @value ||= eval('(' + sanitized_input + '.to_f).round(3)')
    rescue ZeroDivisionError
      'Division by zero is undefined'
    rescue Math::DomainError => e
      e.message
    rescue StandardError, SyntaxError
      nil
    end

    def perform!
      copy_to_clipboard value
    end

    def sanitized_input
      input
        .gsub(/(\D)\./, '\10.')
        .gsub('sleep', '')
        .gsub('exit', '')
    end

    def relevance
      !value.nil?
    end

    def pi
      PI
    end

    def e
      E
    end
  end
end

class Integer
  alias ^ **
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
omnibar-0.0.8 lib/omnibar/queries/calculate.rb