class Criterion::Criteria

Constants

MULTI_VALUE_METHODS
RESULT_METHODS
SINGLE_VALUE_METHODS

Attributes

limit_value[RW]
not_values[RW]
offset_value[RW]
or_values[RW]
order_values[RW]
where_values[RW]

Public Class Methods

new(records) click to toggle source
# File lib/criterion.rb, line 33
def initialize(records)
  @records = records
  MULTI_VALUE_METHODS.each { |v| instance_variable_set(:"@#{v}_values", {}) }
  SINGLE_VALUE_METHODS.each { |v| instance_variable_set(:"@#{v}_value", nil) }
end

Public Instance Methods

all()
Alias for: to_a
average(field) click to toggle source
# File lib/criterion.rb, line 76
def average(field)
  total = count
  return nil if total.zero?
  sum(field) / total.to_f
end
each(&block) click to toggle source
# File lib/criterion.rb, line 128
def each(&block)
  to_a.each(&block)
end
limit(value = true) click to toggle source
# File lib/criterion.rb, line 67
def limit(value = true)
  clone.tap { |r| r.limit_value = value }
end
limit?() click to toggle source
# File lib/criterion.rb, line 114
def limit?
  valid_number?(limit_value)
end
maximum(field) click to toggle source
# File lib/criterion.rb, line 90
def maximum(field)
  to_a.collect { |x| x.send(field) }.max
end
minimum(field) click to toggle source
# File lib/criterion.rb, line 86
def minimum(field)
  to_a.collect { |x| x.send(field) }.min
end
not(query = {}) click to toggle source
# File lib/criterion.rb, line 51
def not(query = {})
  clone.tap do |r|
    r.not_values.merge!(query) unless query.empty?
  end
end
not?() click to toggle source
# File lib/criterion.rb, line 102
def not?
  !not_values.empty?
end
offset(value = true) click to toggle source
# File lib/criterion.rb, line 71
def offset(value = true)
  clone.tap { |r| r.offset_value = value }
end
Also aliased as: skip
offset?() click to toggle source
# File lib/criterion.rb, line 110
def offset?
  valid_number?(offset_value)
end
or(query = {}) click to toggle source
# File lib/criterion.rb, line 45
def or(query = {})
  clone.tap do |r|
    r.or_values.merge!(query) unless query.empty?
  end
end
or?() click to toggle source
# File lib/criterion.rb, line 98
def or?
  !or_values.empty?
end
order(*args) click to toggle source
# File lib/criterion.rb, line 57
def order(*args)
  sort = {}
  args.collect do |arg|
    sort.merge!(arg.is_a?(Hash) ? arg : { arg => :asc })
  end
  clone.tap do |r|
    r.order_values.merge!(sort) unless sort.empty?
  end
end
order?() click to toggle source
# File lib/criterion.rb, line 106
def order?
  !order_values.empty?
end
skip(value = true)
Alias for: offset
sum(field) click to toggle source
# File lib/criterion.rb, line 82
def sum(field)
  to_a.inject(0) { |sum, obj| sum + obj.send(field) }
end
to_a() click to toggle source
# File lib/criterion.rb, line 118
def to_a
  results = @records.select{ |record| keep?(record) }
  results = results.sort_by(&ordering_args) if order?
  results = results.drop(offset_value) if offset?
  results = results.take(limit_value) if limit?
  results
end
Also aliased as: all, to_ary
to_ary()
Alias for: to_a
where(query = {}) click to toggle source
# File lib/criterion.rb, line 39
def where(query = {})
  clone.tap do |r|
    r.where_values.merge!(query) unless query.empty?
  end
end
where?() click to toggle source
# File lib/criterion.rb, line 94
def where?
  !where_values.empty?
end

Private Instance Methods

criteria_matches?(record, values) click to toggle source
# File lib/criterion.rb, line 134
def criteria_matches?(record, values)
  values.all? do |method, value|
    value === record.send(method)
  end
end
keep?(record) click to toggle source
# File lib/criterion.rb, line 140
def keep?(record)
  keep = where? ? criteria_matches?(record, where_values) : true
  alt = or? ? criteria_matches?(record, or_values) : false
  exclude = not? ? criteria_matches?(record, not_values) : false
  (keep || alt) && !exclude
end
ordering_args() click to toggle source
# File lib/criterion.rb, line 147
def ordering_args
  Proc.new do |item|
    order_values.map do |sort|
      next unless [ :asc, :desc ].include?(sort.last)
      sort.last == :desc ? -item.send(sort.first) : item.send(sort.first)
    end
  end
end
valid_number?(value) click to toggle source
# File lib/criterion.rb, line 156
def valid_number?(value)
  return false unless value.is_a?(Integer)
  value >= 0
end