forked from drapergem/draper
/
decorated_enumerable_proxy.rb
47 lines (37 loc) · 1.06 KB
/
decorated_enumerable_proxy.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 Draper
class DecoratedEnumerableProxy
include Enumerable
def initialize(collection, klass, options = {})
@wrapped_collection, @klass, @options = collection, klass, options
end
def each(&block)
@wrapped_collection.each { |member| block.call(@klass.decorate(member, @options)) }
end
def to_ary
@wrapped_collection.map { |member| @klass.decorate(member, @options) }
end
def method_missing (method, *args, &block)
@wrapped_collection.send(method, *args, &block)
end
def respond_to?(method)
super || @wrapped_collection.respond_to?(method)
end
def kind_of?(klass)
super || @wrapped_collection.kind_of?(klass)
end
alias :is_a? :kind_of?
def ==(other)
@wrapped_collection == other
end
def [](index)
@klass.new(@wrapped_collection[index], @options)
end
def to_s
"#<DecoratedEnumerableProxy of #{@klass} for #{@wrapped_collection.inspect}>"
end
def source
@wrapped_collection
end
alias_method :to_source, :source
end
end