Sha256: d16a1e405b7168ab752f120c89e85cadd462fc1a34a3968a780b7203f8ebb41e

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module Draper
  class Base  
    include ActionView::Helpers::TagHelper
    include ActionView::Helpers::UrlHelper
    include ActionView::Helpers::TextHelper

    require 'active_support/core_ext/class/attribute'
    class_attribute :denied, :allowed
    attr_accessor   :source
    
    DEFAULT_DENIED = Object.new.methods << :method_missing
    self.denied = DEFAULT_DENIED
    
    def self.denies(*input_denied)
      raise ArgumentError, "Specify at least one method (as a symbol) to exclude when using denies" if input_denied.empty?
      raise ArgumentError, "Use either 'allows' or 'denies', but not both." if self.allowed?
      self.denied += input_denied
    end
    
    def self.allows(*input_allows)
      raise ArgumentError, "Specify at least one method (as a symbol) to allow when using allows" if input_allows.empty?
      #raise ArgumentError, "Use either 'allows' or 'denies', but not both." unless (self.denies == DEFAULT_EXCLUSIONS)
      self.allowed = input_allows
    end
    
    def initialize(subject)
      self.source = subject      
      build_methods
    end   
    
  private
    def select_methods
      self.allowed || (source.public_methods - denied)
    end

    def build_methods
      select_methods.each do |method|
        (class << self; self; end).class_eval do
          define_method method do |*args|
            source.send method, *args
          end
        end
      end  
    end    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
draper-0.3.1 lib/draper/base.rb