presenter.rb
1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Presenter
# Define presenter base_class
def self.base_class
end
# Define base type condition
def self.available?(instance)
false
end
def self.for(instance)
return instance if instance.is_a?(Presenter) || !available?(instance)
klass = subclasses.sort_by {|class_instance|
class_instance.accepts?(instance) || 0
}.last
klass.accepts?(instance) ? klass.new(instance) : f
end
def initialize(instance)
@instance = instance
end
# Allows to use the original instance reference.
def encapsulated_instance
@instance
end
def id
@instance.id
end
def reload
@instance.reload
self
end
def kind_of?(klass)
@instance.kind_of?(klass)
end
# This method must be overridden in subclasses.
#
# If the class accepts the instance, return a number that represents the
# priority the class should be given to handle that instance. Higher numbers
# mean higher priority.
#
# If the class does not accept the instance, return false.
def self.accepts?(f)
nil
end
# That makes the presenter to works like any other not encapsulated instance.
def method_missing(m, *args)
@instance.send(m, *args)
end
end
# Preload Presenters to allow `Presenter.for()` to work
Dir.glob(File.join('app', 'presenters', '*.rb')) do |file|
load file
end