class ActiveReporter::Dimension::Bin::Set

Public Class Methods

from_hash(source) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 6
def from_hash(source)
  # Returns either a bin or nil, depending on whether the input is valid.
  case source
  when nil
    new(nil, nil)
  when Hash then
    min, max = source.symbolize_keys.values_at(:min, :max)
    new(min.presence, max.presence) unless min.blank? && max.blank?
  else
    nil
  end
end
from_sql(value) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 19
def from_sql(value)
  case value
  when /^([^,]+),(.+)$/ then new($1, $2)
  when /^([^,]+),$/     then new($1, nil)
  when /^,(.+)$/        then new(nil, $1)
  when ',', nil         then new(nil, nil)
  else
    raise "Unexpected SQL bin format #{value}"
  end
end
new(min, max) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 31
def initialize(min, max)
  @min = min
  @max = max
end

Public Instance Methods

==(other) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 129
def ==(other)
  if other.nil?
    min.nil? && max.nil?
  else
    min == other[:min] && max == other[:max]
  end
end
Also aliased as: eql?
[](key) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 105
def [](key)
  case key.to_s
  when 'min' then min
  when 'max' then max
  end
end
as_json(*) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 94
def as_json(*)
  @as_json ||= case bin_edges
  when :min_and_max
    { min: min, max: max }
  when :min
    { min: min }
  when :max
    { max: max }
  end
end
bin_edges() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 138
def bin_edges
  case 
  when min_and_max? then :min_and_max
  when min? then :min
  when max? then :max
  end
end
bin_text() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 64
def bin_text
  "#{min},#{max}"
end
cast(value) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 60
def cast(value)
  quote(value)
end
cast_bin_text() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 68
def cast_bin_text
  case ActiveReporter.database_type
  when :postgres, :sqlite
    "CAST(#{quote(bin_text)} AS text)"
  else
    quote(bin_text)
  end
end
contains_sql(expr) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 81
def contains_sql(expr)
  case bin_edges
  when :min_and_max
    "(#{expr} >= #{quote(min)} AND #{expr} < #{quote(max)})"
  when :min
    "#{expr} >= #{quote(min)}"
  when :max
    "#{expr} < #{quote(max)}"
  else
    "#{expr} IS NULL"
  end
end
eql?(other)
Alias for: ==
has_key?(key) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 112
def has_key?(key)
  %w[min max].include?(key.to_s)
end
Also aliased as: key?
hash() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 125
def hash
  as_json.hash
end
inspect() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 121
def inspect
  "<Bin @min=#{min.inspect} @max=#{max.inspect}>"
end
key?(key)
Alias for: has_key?
max() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 40
def max
  @max && parse(@max)
end
min() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 36
def min
  @min && parse(@min)
end
parse(value) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 52
def parse(value)
  value
end
parses?(value) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 48
def parses?(value)
  parse(value).present? rescue false
end
quote(value) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 56
def quote(value)
  ActiveRecord::Base.connection.quote(value)
end
row_sql() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 77
def row_sql
  "SELECT #{cast(min)} AS min, #{cast(max)} AS max, #{cast_bin_text} AS bin_text"
end
valid?() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 44
def valid?
  (@min.nil? || parses?(@min)) && (@max.nil? || parses?(@max))
end
values_at(*keys) click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 117
def values_at(*keys)
  keys.map { |k| self[key] }
end

Private Instance Methods

max?() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 156
def max?
  max.present?
end
min?() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 152
def min?
  min.present?
end
min_and_max?() click to toggle source
# File lib/active_reporter/dimension/bin/set.rb, line 148
def min_and_max?
  min.present? && max.present?
end