Class: DataFilter::KeywordFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/data_filter/keyword_filter.rb

Overview

Used to filter a data item by a search term by seeing if ANY of the data fields’ values are similar to the search term

Examples:

object = MyModel.new(text: 'hello world', name: 'goodbye', phrase: 'yo')
filter = DataFilter::KeywordFilter.new([:name, :phrase], 'hello')
filter.call(object)
# => nil

Instance Method Summary collapse

Constructor Details

#initialize(field_syms, search_term) ⇒ KeywordFilter

Returns a new instance of KeywordFilter.

Parameters:

  • field_syms (Array<Symbol>)

    a collection of all of the data methods we want to inspect when filtering

  • search_term (String)

    the value we want to use when filtering the data item



15
16
17
18
# File 'lib/data_filter/keyword_filter.rb', line 15

def initialize(field_syms, search_term)
  @field_syms = field_syms
  @search_term = search_term
end

Instance Method Details

#call(item) ⇒ Object

Filters the item

Parameters:

  • item (Comparable)

    the item we want to filter

Returns:

  • (Object)

    the original data item



24
25
26
# File 'lib/data_filter/keyword_filter.rb', line 24

def call(item)
  item if @field_syms.any? { |s| match?(item, s) }
end