Class: Looksy::Lookup
- Inherits:
-
Object
show all
- Defined in:
- lib/looksy/lookup.rb
Instance Method Summary
collapse
Constructor Details
#initialize(klass, cache) ⇒ Lookup
Returns a new instance of Lookup.
3
4
5
6
|
# File 'lib/looksy/lookup.rb', line 3
def initialize(klass, cache)
@klass = klass
@cache = cache
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/looksy/lookup.rb', line 24
def method_missing(method, *args, &block)
if match = Looksy::DynamicFindMatch.match(method)
super unless all_attributes_exist?(match.attributes)
attributes = (match.attributes) do |attribute|
args[match.attributes.index(attribute)]
end
result = all.send(match.finder) do |record|
record_attributes = (match.attributes) do |attribute|
record.send(attribute)
end
record_attributes == attributes
end
match. ? result.send(match.) : result
else
super
end
end
|
Instance Method Details
#all ⇒ Object
8
9
10
|
# File 'lib/looksy/lookup.rb', line 8
def all
@cache.fetch(@klass.cache_key, @klass.cache_options) { @klass.all }
end
|
#find_by_id(id) ⇒ Object
20
21
22
|
# File 'lib/looksy/lookup.rb', line 20
def find_by_id(id)
all.find { |record| record.id == id.to_i }
end
|
#find_first ⇒ Object
12
13
14
|
# File 'lib/looksy/lookup.rb', line 12
def find_first
all.first
end
|
#find_last ⇒ Object
16
17
18
|
# File 'lib/looksy/lookup.rb', line 16
def find_last
all.last
end
|