Sha256: df2bd64026e3f07b9a1c2fc5fd7a7fa6e3495dad82c48e84718d5323ece1f264

Contents?: true

Size: 775 Bytes

Versions: 4

Compression:

Stored size: 775 Bytes

Contents

# frozen_string_literal: true

module TinyFilter
  class Base
    class << self
      def inherited(subclass)
        super
        dup_filters = __filters__.dup
        subclass.__filters__ = dup_filters.each { |key, value| dup_filters[key] = value.dup }
      end

      def filters(key, &block)
        __filters__[key.to_sym] = block
      end

      def filter(base_scope, args = {})
        args.inject(base_scope) do |scope, (key, value)|
          key = key.to_sym
          raise NotDefinedError, "unable to find filter :#{key} in #{self}" unless __filters__.key?(key)

          __filters__[key].call(scope, value)
        end
      end

      protected

      attr_writer :__filters__

      def __filters__
        @__filters__ ||= {}
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tiny_filter-0.3.1 lib/tiny_filter/base.rb
tiny_filter-0.3.0 lib/tiny_filter/base.rb
tiny_filter-0.2.1 lib/tiny_filter/base.rb
tiny_filter-0.2.0 lib/tiny_filter/base.rb