Skip to content

Commit

Permalink
Extraced DEP to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
jcasimir committed Oct 20, 2011
1 parent 5df02e4 commit e644f77
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
1 change: 1 addition & 0 deletions lib/draper.rb
Expand Up @@ -4,5 +4,6 @@
require 'draper/lazy_helpers'
require 'draper/model_support'
require 'draper/view_context'
require 'draper/decorated_enumerable_proxy'

Draper::System.setup
31 changes: 2 additions & 29 deletions lib/draper/base.rb
Expand Up @@ -93,7 +93,7 @@ def self.allows(*input_allows)
# @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
Expand Down Expand Up @@ -146,33 +146,6 @@ def self.respond_to?(method, include_private = false)
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 changes: 27 additions & 0 deletions lib/draper/decorated_enumerable_proxy.rb
@@ -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

0 comments on commit e644f77

Please sign in to comment.