Sha256: f7203e69a2626a5d23444b683dd4075188826385266a56844b2227bc9686bb14

Contents?: true

Size: 1.06 KB

Versions: 6

Compression:

Stored size: 1.06 KB

Contents

module Repor
  module Dimensions
    class BinDimension
      class BinTable < Array
        def initialize(values)
          super(values.compact)
        end

        def filter(relation, expr)
          relation.where(any_contain(expr))
        end

        def group(relation, expr, value_name)
          name = "#{value_name}_bin_table"

          bin_join = <<-SQL
INNER JOIN (
  #{rows.join(" UNION\n  ")}
) AS #{name} ON (
  CASE
  WHEN #{name}.min IS NULL AND #{name}.max IS NULL THEN (#{expr} IS NULL)
  WHEN #{name}.min IS NULL THEN (#{expr} < #{name}.max)
  WHEN #{name}.max IS NULL THEN (#{expr} >= #{name}.min)
  ELSE ((#{expr} >= #{name}.min) AND (#{expr} < #{name}.max))
  END
)
          SQL
          
          selection = "#{name}.bin_text AS #{value_name}"

          relation.
            joins(bin_join).
            select(selection).
            group(value_name)
        end

        def rows
          map(&:row_sql)
        end

        def any_contain(expr)
          map { |bin| bin.contains_sql(expr) }.join(' OR ')
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
repor-0.1.0 lib/repor/dimensions/bin_dimension/bin_table.rb
repor-0.0.5 lib/repor/dimensions/bin_dimension/bin_table.rb
repor-0.0.4 lib/repor/dimensions/bin_dimension/bin_table.rb
repor-0.0.3 lib/repor/dimensions/bin_dimension/bin_table.rb
repor-0.0.2 lib/repor/dimensions/bin_dimension/bin_table.rb
repor-0.0.1 lib/repor/dimensions/bin_dimension/bin_table.rb