Sha256: 4d440bed2f2d9fe6a8ddd727a0dd23d86df6b02832ec726d7532b7c3b26dc53a

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 KB

Contents

module Capcode
  class << self
    # Add a before filter :
    #
    #   module Capcode
    #     before_filter :my_global_action
    #     before_filter :need_login, :except => [:Login]
    #     before_filter :check_mail, :only => [:MailBox]
    #     # ...
    #   end
    #
    # If the action return nil, the normal get or post will be executed, else no.
    # 
    def before_filter( action, opts = {} )
      Capcode::Filter.filters[action] = { }
      
      opts.each do |k, v|
        Capcode::Filter.filters[action][k] = v
      end
    end
  end
  
  class Filter #:nodoc:
    class << self
      def filters #:nodoc:
        @filters ||= { }
      end
      
      def execute( klass ) #:nodoc:
        klass_sym = "#{klass.class}".split( /::/)[-1].to_sym
        actions = []
        filters.each do |action, data|
          if (data[:only] and data[:only].include?(klass_sym)) or 
             (data[:except] and not data[:except].include?(klass_sym)) or
             (data.keys.size == 0)
            actions << action
          end
        end

        klass.class.instance_eval{ include Capcode }
        rCod = nil
        actions.each do |a|
          rCod = klass.send( a )
        end
        
        return rCod
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
Capcode-0.9.7 lib/capcode/filters.rb
Capcode-0.9.6 lib/capcode/filters.rb
Capcode-0.9.5 lib/capcode/filters.rb
Capcode-0.9.4 lib/capcode/filters.rb
Capcode-0.9.3 lib/capcode/filters.rb