class ActiveHash::Base::WhereChain

Public Class Methods

new(scope) click to toggle source
# File lib/active_hash/base.rb, line 20
def initialize(scope)
  @scope = scope
  @records = @scope.all
end

Public Instance Methods

not(options) click to toggle source
# File lib/active_hash/base.rb, line 25
def not(options)
  return @scope if options.blank?

  # use index if searching by id
  if options.key?(:id) || options.key?("id")
    ids = @scope.pluck(:id) - Array.wrap(options.delete(:id) || options.delete("id"))
    candidates = ids.map { |id| @scope.find_by_id(id) }.compact
  end
  return candidates if options.blank?

  filtered_records = (candidates || @records || []).reject do |record|
    match_options?(record, options)
  end
  
  ActiveHash::Relation.new(@scope.klass, filtered_records, {})
end

Private Instance Methods

match_options?(record, options) click to toggle source
# File lib/active_hash/base.rb, line 42
def match_options?(record, options)
  options.all? do |col, match|
    if match.kind_of?(Array)
      match.any? { |v| normalize(v) == normalize(record[col]) }
    else
      normalize(record[col]) == normalize(match)
    end
  end
end
normalize(v) click to toggle source
# File lib/active_hash/base.rb, line 54
def normalize(v)
  v.respond_to?(:to_sym) ? v.to_sym : v
end