mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
Extraced DEP to its own file
This commit is contained in:
parent
5df02e428d
commit
e644f777d2
3 changed files with 30 additions and 29 deletions
|
@ -4,5 +4,6 @@ require 'draper/base'
|
|||
require 'draper/lazy_helpers'
|
||||
require 'draper/model_support'
|
||||
require 'draper/view_context'
|
||||
require 'draper/decorated_enumerable_proxy'
|
||||
|
||||
Draper::System.setup
|
|
@ -93,7 +93,7 @@ module Draper
|
|||
# @param [Object] instance(s) to wrap
|
||||
# @param [Object] context (optional)
|
||||
def self.decorate(input, context = {})
|
||||
input.respond_to?(:each) ? DecoratedEnumerableProxy.new(input, self, context) : new(input, context)
|
||||
input.respond_to?(:each) ? Draper::DecoratedEnumerableProxy.new(input, self, context) : new(input, context)
|
||||
end
|
||||
|
||||
# Access the helpers proxy to call built-in and user-defined
|
||||
|
@ -146,33 +146,6 @@ module Draper
|
|||
private
|
||||
def allow?(method)
|
||||
(!allowed? || allowed.include?(method) || FORCED_PROXY.include?(method)) && !denied.include?(method)
|
||||
end
|
||||
|
||||
class DecoratedEnumerableProxy
|
||||
include Enumerable
|
||||
|
||||
def initialize(collection, klass, context)
|
||||
@wrapped_collection, @klass, @context = collection, klass, context
|
||||
end
|
||||
|
||||
# Implementation of Enumerable#each that proxyes to the wrapped collection
|
||||
def each(&block)
|
||||
@wrapped_collection.each { |member| block.call(@klass.new(member, @context)) }
|
||||
end
|
||||
|
||||
# Implement to_arry so that render @decorated_collection is happy
|
||||
def to_ary
|
||||
@wrapped_collection.to_ary
|
||||
end
|
||||
|
||||
def method_missing (meth, *args, &block)
|
||||
@wrapped_collection.send(meth, *args, &block)
|
||||
end
|
||||
|
||||
def to_s
|
||||
"#<DecoratedEnumerableProxy of #{@klass} for #{@wrapped_collection.inspect}>"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
27
lib/draper/decorated_enumerable_proxy.rb
Normal file
27
lib/draper/decorated_enumerable_proxy.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
module Draper
|
||||
class DecoratedEnumerableProxy
|
||||
include Enumerable
|
||||
|
||||
def initialize(collection, klass, context)
|
||||
@wrapped_collection, @klass, @context = collection, klass, context
|
||||
end
|
||||
|
||||
# Implementation of Enumerable#each that proxyes to the wrapped collection
|
||||
def each(&block)
|
||||
@wrapped_collection.each { |member| block.call(@klass.new(member, @context)) }
|
||||
end
|
||||
|
||||
# Implement to_arry so that render @decorated_collection is happy
|
||||
def to_ary
|
||||
@wrapped_collection.to_ary
|
||||
end
|
||||
|
||||
def method_missing (meth, *args, &block)
|
||||
@wrapped_collection.send(meth, *args, &block)
|
||||
end
|
||||
|
||||
def to_s
|
||||
"#<DecoratedEnumerableProxy of #{@klass} for #{@wrapped_collection.inspect}>"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue