4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/chooser.rb', line 4
def choose(selector)
ary = []
if selector.is_a?(Hash)
each do |ob|
ary << ob if selector.all? do |m, v|
ob.public_send(m) == v ||
(v.respond_to?(:include?) && v.include?(ob.public_send(m))) ||
(v.is_a?(Regexp) && v =~ ob.public_send(m))
end
end
elsif selector.is_a?(String)
each do |ob|
ary << ob if !!ob.instance_eval(selector)
end
else
raise ArgumentError, 'Supply an options hash (e.g. target.choose :street => "Main", :age => (24..30), :address => /Main/) or evaluated string (e.g. target.choose "age >= 24 && address =~ /Main/").'
end
ary
end
|