class Airbrake::FilterChain
FilterChain
represents an ordered array of filters.
A filter is an object that responds to #call (typically a Proc or a class that implements the call method). The #call method must accept exactly one argument: an object to be filtered.
When you add a new filter to the chain, it gets inserted according to its weight. Smaller weight means the filter will be somewhere in the beginning of the array. Larger - in the end. If a filter doesn’t implement weight, the chain assumes it’s equal to 0.
@example
class MyFilter attr_reader :weight def initialize @weight = 1 end def call(obj) puts 'Filtering...' obj[:data] = '[Filtered]' end end filter_chain = FilterChain.new filter_chain.add_filter(MyFilter) filter_chain.refine(obj) #=> Filtering...
@see Airbrake.add_filter
@api private @since v1.0.0
Constants
- DEFAULT_WEIGHT
@return [Integer]
Public Class Methods
# File lib/airbrake-ruby/filter_chain.rb, line 40 def initialize @filters = [] end
Public Instance Methods
Adds a filter to the filter chain. Sorts filters by weight.
@param [#call] filter The filter object (proc, class, module, etc) @return [void]
# File lib/airbrake-ruby/filter_chain.rb, line 48 def add_filter(filter) @filters = (@filters << filter).sort_by do |f| f.respond_to?(:weight) ? f.weight : DEFAULT_WEIGHT end.reverse! end
Deletes a filter from the the filter chain.
@param [Class] filter_class The class of the filter you want to delete @return [void] @since v3.1.0
# File lib/airbrake-ruby/filter_chain.rb, line 59 def delete_filter(filter_class) # rubocop:disable Style/ClassEqualityComparison index = @filters.index { |f| f.class.name == filter_class.name } # rubocop:enable Style/ClassEqualityComparison @filters.delete_at(index) if index end
@param [Class] filter_class @return [Boolean] true if the current chain has an instance of the given
class, false otherwise
@since v4.14.0
# File lib/airbrake-ruby/filter_chain.rb, line 102 def includes?(filter_class) filter_classes.include?(filter_class) end
@return [String] customized inspect to lessen the amount of clutter
# File lib/airbrake-ruby/filter_chain.rb, line 81 def inspect filter_classes.to_s end
@return [String] {#inspect} for PrettyPrint
# File lib/airbrake-ruby/filter_chain.rb, line 86 def pretty_print(q) q.text('[') # Make nesting of the first element consistent on JRuby and MRI. q.nest(2) { q.breakable } if @filters.any? q.nest(2) do q.seplist(@filters) { |f| q.pp(f.class) } end q.text(']') end
Applies all the filters in the filter chain to the given notice. Does not filter ignored notices.
@param [Airbrake::Notice] notice The notice to be filtered @return [void] @todo Make it work with anything, not only notices
# File lib/airbrake-ruby/filter_chain.rb, line 72 def refine(notice) @filters.each do |filter| break if notice.ignored? filter.call(notice) end end
Private Instance Methods
# File lib/airbrake-ruby/filter_chain.rb, line 108 def filter_classes @filters.map(&:class) end