Class: DataFilter::PrefixFilter

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

Overview

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

Examples:

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

Instance Method Summary collapse

Constructor Details

#initialize(field_sym, prefix) ⇒ PrefixFilter



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

def initialize(field_sym, prefix)
  @field_sym = field_sym
  @prefix = prefix
end

Instance Method Details

#call(item) ⇒ Object?

Filters the item



24
25
26
27
28
29
# 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