Sha256: 2c67c0ad5c7d61c263f0832cb58425e8c9a62133e91765d0b5f92c7db858789f

Contents?: true

Size: 710 Bytes

Versions: 4

Compression:

Stored size: 710 Bytes

Contents

module ActiveMedian
  class SQLiteHandler
    def self.arity
      2
    end

    def self.name
      "percentile"
    end

    def initialize
      @values = []
      @percentile = nil
    end

    # skip checks for
    # 1. percentile between 0 and 100
    # 2. percentile same for all rows
    # since input is already checked
    def step(ctx, value, percentile)
      return if value.nil?
      raise ActiveRecord::StatementInvalid, "1st argument to percentile() is not numeric" unless value.is_a?(Numeric)
      @percentile ||= percentile
      @values << value
    end

    def finalize(ctx)
      if @values.any?
        ctx.result = @values.percentile(@percentile / 100.0)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_median-0.5.0 lib/active_median/sqlite_handler.rb
active_median-0.4.1 lib/active_median/sqlite_handler.rb
active_median-0.4.0 lib/active_median/sqlite_handler.rb
active_median-0.3.3 lib/active_median/sqlite_handler.rb