Sha256: 594b7b19a42ad098a7adf332c72497bac6d66a0755fcbb7a4b64865451666d8e

Contents?: true

Size: 1.19 KB

Versions: 10

Compression:

Stored size: 1.19 KB

Contents

# This gear is originally from filtering_camping gem by judofyr, and techarc.
module Gear
  module Filters
    module ClassMethods
      def filters
        @_filters ||= {:before => [], :after => []}
      end

      def before(actions, &blk)
        actions = [actions] unless actions.is_a?(Array)
        actions.each do |action|
          filters[:before] << [action, blk]
        end
      end

      def after(actions, &blk)
        actions = [actions] unless actions.is_a?(Array)
        actions.each do |action|
          filters[:after] << [action, blk]
        end
      end
    end

    def self.included(mod)
      mod.extend(ClassMethods)
    end

    def self.setup(app, *a, &block) end

    def run_filters(type)
      o = self.class.to_s.split("::")
      filters = Object.const_get(o.first).filters
      filters[type].each do |filter|
        if (filter[0].is_a?(Symbol) && (filter[0] == o.last.to_sym || filter[0] == :all)) ||
           (filter[0].is_a?(String) && /^#{filter[0]}\/?$/ =~ @env.REQUEST_URI)
           self.instance_eval(&filter[1])
        end
      end
    end

    def service(*a)
      run_filters(:before)
      super(*a)
      run_filters(:after)
      self
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
camping-3.2.6 lib/gear/filters.rb
camping-3.2.5 lib/camping/gear/filters.rb
camping-3.2.4 lib/camping/gear/filters.rb
camping-3.2.3 lib/camping/gear/filters.rb
camping-3.2.2 lib/camping/gear/filters.rb
camping-3.2.1 lib/camping/gear/filters.rb
camping-3.2.0 lib/camping/gear/filters.rb
camping-3.1.3 lib/camping/gear/filters.rb
camping-3.1.2 lib/camping/gear/filters.rb
camping-3.1.0 lib/camping/gear/filters.rb