Class: DynamicFindersPlus::Finder
- Inherits:
-
Object
- Object
- DynamicFindersPlus::Finder
show all
- Defined in:
- lib/dynamic_finders_plus/finder.rb
Class Method Summary
collapse
Class Method Details
.define_for(klass) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/dynamic_finders_plus/finder.rb', line 23
def self.define_for klass
return nil unless defined.add? klass
return if klass.abstract_class
results = {}
klass.columns.each do |column|
(finders[column.type] || []).each do |name_l, body|
wrapped= ->(*args) do
args.insert(0, klass, column);
body.call *args
end
name = name_l.(column)
method = klass.define_singleton_method name, &wrapped
results[name.to_sym] = method
end
end
results
end
|
.defined ⇒ Object
43
44
45
|
# File 'lib/dynamic_finders_plus/finder.rb', line 43
def self.defined
@@defined ||= Set.new
end
|
.finder(types, name_lambda, body_lambda = nil, &body_block) ⇒ Object
14
15
16
17
18
19
20
21
|
# File 'lib/dynamic_finders_plus/finder.rb', line 14
def self.finder (types, name_lambda, body_lambda=nil, &body_block)
body_lambda ||= body_block.to_proc
wrap(types).each do |type|
type= type.to_sym
finders[type] ||= Set.new
finders[type] << [name_lambda, body_lambda]
end
end
|
.finders ⇒ Object
47
48
49
|
# File 'lib/dynamic_finders_plus/finder.rb', line 47
def self.finders
@@finders ||= {}
end
|
.wrap(object) ⇒ Object
4
5
6
7
8
9
10
11
12
|
# File 'lib/dynamic_finders_plus/finder.rb', line 4
def self.wrap object
if object.nil?
[]
elsif object.respond_to?(:to_ary)
object.to_ary || [object]
else
[object]
end
end
|