Module: Phut::Finder

Included in:
Netns, OpenVswitch, Vhost
Defined in:
lib/phut/finder.rb

Overview

Defines find_by method

Instance Method Summary collapse

Instance Method Details

#find_by(queries) ⇒ Object



8
9
10
11
12
# File 'lib/phut/finder.rb', line 8

def find_by(queries)
  queries.inject(all) do |memo, (attr, value)|
    memo.find_all { |each| each.__send__(attr) == value }
  end.first
end

#find_by!(queries) ⇒ Object



14
15
16
17
# File 'lib/phut/finder.rb', line 14

def find_by!(queries)
  name = to_s.demodulize
  find_by(queries) || raise("#{name} #{queries.inspect} not found")
end