class QueryRelation

Constants

VERSION

Attributes

klass[R]
options[RW]

Public Class Methods

new(model, opts = nil, &block) click to toggle source
  • bind

  • create_with

  • distinct

  • eager_load

  • X

    except ? - is this not defined in the interface?

  • extending

  • from

  • X

    group

  • ~~having~~ - NO

  • X

    includes (partial)

  • joins

  • X

    limit

  • lock

  • .

    none

  • X

    offset

  • X

    order (partial)

  • preload

  • readonly

  • X

    references (partial)

  • X

    reorder

  • reverse_order

  • X

    select (partial)

  • X

    unscope

  • uniq

  • X

    where (partial)

  • where.not

# File lib/query_relation.rb, line 45
def initialize(model, opts = nil, &block)
  @klass   = model
  @options = opts ? opts.dup : {}
  @target = block || ->(*args) { klass.send(:search, *args) }
end

Public Instance Methods

all() click to toggle source
# File lib/query_relation.rb, line 148
def all
  self
end
count(*_args) click to toggle source

count(:all) is very common but [1, 2, 3].count(:all) == 0

# File lib/query_relation.rb, line 154
def count(*_args)
  to_a.size
end
except(*val) click to toggle source
# File lib/query_relation.rb, line 114
def except(*val)
  dup.tap do |r|
    val.flatten.compact.each do |key|
      r.options.delete(key)
    end
  end
end
first() click to toggle source

TODO: support arguments

# File lib/query_relation.rb, line 161
def first
  defined?(@results) ? @results.first : call_query_method(:first)
end
group(*args) click to toggle source
# File lib/query_relation.rb, line 99
def group(*args)
  append_hash_arg :group, *args
end
includes(*args) click to toggle source
# File lib/query_relation.rb, line 75
def includes(*args)
  append_hash_array_arg :includes, {}, *args
end
instances_are_derived?() click to toggle source
# File lib/query_relation.rb, line 170
def instances_are_derived?
  true
end
last() click to toggle source

TODO: support arguments

# File lib/query_relation.rb, line 166
def last
  defined?(@results) ? @results.last : call_query_method(:last)
end
limit(val) click to toggle source
# File lib/query_relation.rb, line 83
def limit(val)
  assign_arg :limit, val
end
limit_value() click to toggle source
# File lib/query_relation.rb, line 87
def limit_value
  options[:limit]
end
offset(val) click to toggle source
# File lib/query_relation.rb, line 131
def offset(val)
  assign_arg :offset, val
end
offset_value() click to toggle source
# File lib/query_relation.rb, line 135
def offset_value
  options[:offset]
end
order(*args) click to toggle source
# File lib/query_relation.rb, line 91
def order(*args)
  append_hash_array_arg :order, "ASC", *args
end
order_values() click to toggle source
# File lib/query_relation.rb, line 95
def order_values
  options[:order] || []
end
references(*args) click to toggle source
# File lib/query_relation.rb, line 79
def references(*args)
  append_hash_array_arg :references, {}, *args
end
reorder(*val) click to toggle source
# File lib/query_relation.rb, line 103
def reorder(*val)
  val = val.flatten.compact
  if val.first.kind_of?(Hash)
    raise ArgumentError, "Need to support #{__callee__}(#{val.class.name})"
  end

  dup.tap do |r|
    r.options[:order] = val
  end
end
select(*args) click to toggle source

@param val [Array<Sting,Symbol>,String, Symbol]

# File lib/query_relation.rb, line 140
def select(*args)
  append_hash_arg :select, *args
end
to_a() click to toggle source
# File lib/query_relation.rb, line 144
def to_a
  @results ||= call_query_method(:all)
end
unscope(*val) click to toggle source

similar to except. difference being this persists across merges

# File lib/query_relation.rb, line 123
def unscope(*val)
  dup.tap do |r|
    val.flatten.compact.each do |key|
      r.options[key] = nil
    end
  end
end
where(*val) click to toggle source
# File lib/query_relation.rb, line 51
def where(*val)
  val = val.flatten.compact
  val = val.first if val.size == 1 && val.first.kind_of?(Hash)
  dup.tap do |r|
    old_where = r.options[:where]
    if val.blank?
      # nop
    elsif old_where.blank?
      r.options[:where] = val
    elsif old_where.kind_of?(Hash) && val.kind_of?(Hash)
      val.each_pair do |key, value|
        old_where[key] = if old_where[key]
                           Array.wrap(old_where[key]) + Array.wrap(value)
                         else
                           value
                         end
      end
    else
      raise ArgumentError,
            "Need to support #{__callee__}(#{val.class.name}) with existing #{old_where.class.name}"
    end
  end
end

Private Instance Methods

append_hash_arg(symbol, *val) click to toggle source
# File lib/query_relation.rb, line 184
def append_hash_arg(symbol, *val)
  val = val.flatten.compact
  if val.first.kind_of?(Hash)
    raise ArgumentError, "Need to support #{symbol}(#{val.class.name})"
  end
  dup.tap do |r|
    r.options[symbol] = r.options[symbol] ? (r.options[symbol] + val) : val
  end
end
append_hash_array_arg(symbol, default, *val) click to toggle source
# File lib/query_relation.rb, line 194
def append_hash_array_arg(symbol, default, *val)
  val = val.flatten.compact
  val = val.first if val.size == 1 && val.first.kind_of?(Hash)
  dup.tap do |r|
    r.options[symbol] = merge_hash_or_array(r.options[symbol], val, default)
  end
end
array_to_hash(array, default = {}) click to toggle source

This takes the array form and converts into the equivalent hash form

@example converting an order parameter

# Vm.order(:name, :ip)
array_to_hash([:name, :ip], "ASC") #=> {:name => "ASC", :ip => "ASC"}

@example converting an includes parameter

# Vm.includes([:ext_management_system, :host])
array_to_hash([:ext_management_system, :host], {}) #=> {:ext_management_system => {}, :host =>{}}

@param array [Array<Symbol>] array of names @param default value to be associated with each object (i.e.: “ASC”, {}) @return [Hash{Symbol => String, Hash}] Hash equivalent of the input array

# File lib/query_relation.rb, line 232
def array_to_hash(array, default = {})
  array.each_with_object({}) { |k, h| h[k] = default.dup }
end
assign_arg(symbol, val) click to toggle source
# File lib/query_relation.rb, line 236
def assign_arg(symbol, val)
  dup.tap do |r|
    r.options[symbol] = val
  end
end
call_query_method(mode) click to toggle source
# File lib/query_relation.rb, line 180
def call_query_method(mode)
  @target.call(mode, options.delete_blanks)
end
dup() click to toggle source
# File lib/query_relation.rb, line 176
def dup
  self.class.new(klass, options, &@target)
end
merge_hash_or_array(a, b, default = {}) click to toggle source

@param a [Array, Hash] @param b [Array, Hash] @param default default value for conversion to a hash. e.g.: {} or “ASC”

# File lib/query_relation.rb, line 205
def merge_hash_or_array(a, b, default = {})
  if a.blank?
    b
  elsif b.blank?
    a
  elsif a.kind_of?(Array) && b.kind_of?(Array)
    a + b
  else
    a = array_to_hash(a, default) if a.kind_of?(Array)
    b = array_to_hash(b, default) if b.kind_of?(Array)
    a.merge(b)
  end
end