class DataFilter::LikeFilter

Used to filter a data item by a search term by seeing if the data field value is similar to the search term

@example

object = MyModel.new(text: 'hello world!')
filter = DataFilter::LikeFilter.new(:text, 'hello')
filter.call(object)
# => #<MyModel text: 'hello world'>

Public Class Methods

new(field_sym, search_term, normalize_regex = nil) click to toggle source

@param field_sym [Symbol] name of the data method we want

to filter

@param search_term [String] the value we want to use when

filtering the data item

@param normalize_regex [regex] the optional regular

expression for normalizing the string to search
# File lib/data_filter/like_filter.rb, line 17
def initialize(field_sym, search_term, normalize_regex = nil)
  @field_sym = field_sym
  @search_term = search_term
  @normalize_regex = normalize_regex || /[^\w\s]/
end

Public Instance Methods

call(item) click to toggle source

Filters the item

@param item [Object] the item we want to filter @return [Object, nil] the original data item

# File lib/data_filter/like_filter.rb, line 27
def call(item)
  if item.respond_to?(@field_sym) &&
    match?(item.public_send(@field_sym), @search_term)
    item
  end
end