-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathcollection.rb
47 lines (39 loc) · 1.22 KB
/
collection.rb
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
module Cell
class Collection
def initialize(ary, options, cell_class)
@method = options.delete(:method) # TODO: deprecate :method.
@join = options.delete(:collection_join)
@ary = ary
@options = options
@cell_class = cell_class
end
module Call
def call(state=:show)
join(@join) { |cell, i| cell.(@method || state) }.
html_safe # FIXME: this is not desired outside of rails.
end
end
include Call
alias to_s call
# Iterate collection and build a cell for each item.
# The passed block receives that cell and the index.
# Its return value is captured and joined.
def join(separator="", &block)
@ary.each_with_index.collect do |model, i|
yield @cell_class.build(model, @options), i
end.
join(separator)
end
module Layout
def call(*) # WARNING: THIS IS NOT FINAL API.
blaaaa_layout = @options.delete(:layout) # FIXME: THAT SUCKS.
content = super # DISCUSS: that could come in via the pipeline argument.
ViewModel::Layout::External::Render.(content, @ary, blaaaa_layout, @options)
end
end
include Layout
end
end
# Collection#call
# |> Header#call
# |> Layout#call