Module: ActiveHash::ARApi::FindBy

Included in:
ActiveMocker::ActiveHash::ARApi::ClassMethods
Defined in:
lib/active_hash/find_by.rb

Instance Method Summary collapse

Instance Method Details

#find_by(options = {}) ⇒ Object



8
9
10
# File 'lib/active_hash/find_by.rb', line 8

def find_by(options = {})
  send(:where, options).first
end

#find_by!(options = {}) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


12
13
14
15
16
# File 'lib/active_hash/find_by.rb', line 12

def find_by!(options={})
  result = find_by(options)
  raise ActiveRecord::RecordNotFound if result.blank?
  result
end

#where(options) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/active_hash/find_by.rb', line 18

def where(options)
  return @records if options.nil?
  (@records || []).select do |record|
    options.all? do |col, match|
      record.send(col) == match
    end
  end
end