Sha256: 9b29f89eaedfa77f28dab059ac2d9ac3aa4e9d1d323a17a3eca0b62b9a5714d8

Contents?: true

Size: 1015 Bytes

Versions: 4

Compression:

Stored size: 1015 Bytes

Contents

require 'bjn_inventory/default_logger'

module BjnInventory

    module Util

        class Filter

            def initialize(kwargs={})
                @logger = kwargs.delete(:logger) || BjnInventory::DefaultLogger.new
                unless kwargs.has_key? :filters
                    raise RuntimeError, "#{self.class} requires :filters to be set"
                end
                @filters = kwargs[:filters]
                set_options(kwargs)
            end

            def set_options(kwargs={})
                @options = kwargs
            end

            def logger()
                @logger
            end

            def match(entity)
                if @filters.respond_to? :call
                    @filters.call entity
                else
                    raise RuntimeError, "#{self.class} requires :filters to be callable"
                end
            end

            def select(arr)
                arr.select { |entity| match entity }
            end

        end

    end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bjn_inventory-1.7.1 lib/bjn_inventory/util/filter.rb
bjn_inventory-1.7.0 lib/bjn_inventory/util/filter.rb
bjn_inventory-1.6.1 lib/bjn_inventory/util/filter.rb
bjn_inventory-1.5.1 lib/bjn_inventory/util/filter.rb