class DataFilter::PrefixFilter

Used to filter a data item by a prefix by seeing if the data field value starts with the prefix

@example

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

Public Class Methods

new(field_sym, prefix) click to toggle source

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

to filter

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

filtering the data item
# File lib/data_filter/prefix_filter.rb, line 15
def initialize(field_sym, prefix)
  @field_sym = field_sym
  @prefix = prefix
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/prefix_filter.rb, line 24
def call(item)
  if item.respond_to?(@field_sym) &&
    starts_with?(item.public_send(@field_sym), @prefix)
    item
  end
end

Private Instance Methods

starts_with?(actual, prefix) click to toggle source
# File lib/data_filter/prefix_filter.rb, line 33
def starts_with?(actual, prefix)
  actual.match(/\A#{prefix}/i)
end