class Repor::Dimensions::BinDimension::Bin

Public Class Methods

from_hash(h) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 79
def self.from_hash(h)
  # Returns either a bin or nil, depending on whether
  # the input is valid.
  return new(nil, nil) if h.nil?
  return unless h.is_a?(Hash)
  min, max = h.symbolize_keys.values_at(:min, :max)
  return if min.blank? && max.blank?
  new(min.presence, max.presence)
end
from_sql(value) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 68
def self.from_sql(value)
  case value
  when /^([^,]+),(.+)$/ then new($1, $2)
  when /^([^,]+),$/     then new($1, nil)
  when /^,(.+)$/        then new(nil, $1)
  when ','              then new(nil, nil)
  else
    raise "Unexpected SQL bin format #{value}"
  end
end
new(min, max) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 5
def initialize(min, max)
  @min = min
  @max = max
end

Public Instance Methods

==(other) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 125
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/repor/dimensions/bin_dimension/bin.rb, line 102
def [](key)
  return min if key.to_s == 'min'
  return max if key.to_s == 'max'
end
as_json(*) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 89
def as_json(*)
  return @as_json if instance_variable_defined?(:@as_json)
  @as_json = if min && max
    { min: min, max: max }
  elsif min
    { min: min }
  elsif max
    { max: max }
  else
    nil
  end
end
bin_text() click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 39
def bin_text
  "#{min},#{max}"
end
cast(value) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 35
def cast(value)
  quote(value)
end
cast_bin_text() click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 43
def cast_bin_text
  case Repor.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/repor/dimensions/bin_dimension/bin.rb, line 56
def contains_sql(expr)
  if min && max
    "(#{expr} >= #{quote(min)} AND #{expr} < #{quote(max)})"
  elsif max
    "#{expr} < #{quote(max)}"
  elsif min
    "#{expr} >= #{quote(min)}"
  else
    "#{expr} IS NULL"
  end
end
eql?(other)
Alias for: ==
has_key?(key) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 107
def has_key?(key)
  key.to_s == 'min' || key.to_s == 'max'
end
Also aliased as: key?
hash() click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 121
def hash
  as_json.hash
end
inspect() click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 117
def inspect
  "<Bin @min=#{min.inspect} @max=#{max.inspect}>"
end
key?(key)
Alias for: has_key?
max() click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 14
def max
  @max && parse(@max)
end
min() click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 10
def min
  @min && parse(@min)
end
parse(value) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 27
def parse(value)
  value
end
parses?(value) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 23
def parses?(value)
  parse(value).present? rescue false
end
quote(value) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 31
def quote(value)
  ActiveRecord::Base.connection.quote(value)
end
row_sql() click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 52
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/repor/dimensions/bin_dimension/bin.rb, line 18
def valid?
  (@min.nil? || parses?(@min)) &&
    (@max.nil? || parses?(@max))
end
values_at(*keys) click to toggle source
# File lib/repor/dimensions/bin_dimension/bin.rb, line 113
def values_at(*keys)
  keys.map { |k| self[key] }
end