Module: DataFilter::FilterSet::DSL

Included in:
DataFilter::FilterSet
Defined in:
lib/data_filter/filter_set.rb

Overview

A DSL for creating a series of filters that can be called

Provides a cleaner way to define a DataFilter::FilterSet with a bunch of different filters

Conditionally adds filters to the set based on whether or not any valid search terms are provided (useful for Controller params)

Examples:

Office365::Mail::MessagesController

filter_set = DataFilter::FilterSet.create do
  like_filter :to,        by: params[:to]
  like_filter :from,      by: params[:from]
  like_filter :cc,        by: params[:cc]
  like_filter :bcc,       by: params[:bcc]
  like_filter :subject,   by: params[:subject]

  keyword_filter [:to, :from, :cc, :bcc, :subject], by: params[:keyword]

  range_filter :date, floor: start_date, ceiling: end_date

  if params[:has_attachment] === true
    range_filter :attachment_count, floor: 1
  elsif params[:has_attachment] === false
    range_filter :attachment_count, ceiling: 0, nil_default: 0
  end
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Used to support the DSL. Calls out to the parent scope if we receive a message we can’t respond to



177
178
179
# File 'lib/data_filter/filter_set.rb', line 177

def method_missing(sym, *args, &block)
  @original_self.send(sym, *args, &block)
end

Class Method Details

.included(base) ⇒ Object



68
69
70
# File 'lib/data_filter/filter_set.rb', line 68

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#keyword_filter(field_syms, opts = {}) ⇒ Object

Adds a KeywordFilter to the filter set

Parameters:

  • field_syms (Array<Symbol>)

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

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :by (Object)

    the value we want to use when filtering the data item



120
121
122
123
124
# File 'lib/data_filter/filter_set.rb', line 120

def keyword_filter(field_syms, opts = {})
  if opts[:by]
    @filters << KeywordFilter.new(field_syms, opts[:by])
  end
end

#like_filter(field_sym, opts = {}) ⇒ Object

Adds a LikeFilter to the filter set

Parameters:

  • field_sym (Symbol)

    name of the data method we want to filter

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :by (Object)

    the value we want to use when filtering the data item, :normalize_regex the regular expression used to normalize the string



96
97
98
99
100
# File 'lib/data_filter/filter_set.rb', line 96

def like_filter(field_sym, opts = {})
  if opts[:by]
    @filters << LikeFilter.new(field_sym, opts[:by], opts[:normalize_regex])
  end
end

#prefix_filter(field_sym, opts = {}) ⇒ Object

Adds a PrefixFilter to the filter set

Parameters:

  • field_sym (Symbol)

    name of the data method we want to filter

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :by (Object)

    the value we want to use when filtering the data item



108
109
110
111
112
# File 'lib/data_filter/filter_set.rb', line 108

def prefix_filter(field_sym, opts = {})
  if opts[:by]
    @filters << PrefixFilter.new(field_sym, opts[:by])
  end
end

#range_filter(field_sym, opts = {}) ⇒ Object

Adds a RangeFilter to the filter set

Parameters:

  • field_sym (Symbol)

    name of the data method we want to filter

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :floor (Comparable)

    the range beginning we want to filter the data item by

  • :ceiling (Comparable)

    the range end we want to filter the data item by

  • :nil_default (Comparable)

    the value to use if the data item has no field value



136
137
138
139
140
# File 'lib/data_filter/filter_set.rb', line 136

def range_filter(field_sym, opts = {})
  if opts[:floor] || opts[:ceiling]
    @filters << RangeFilter.new(field_sym, **opts)
  end
end

#range_overlap_filter(start_sym, end_sym, opts = {}) ⇒ Object

Adds a RangeOverlapFilter to the filter set

Parameters:

  • start_sym (Symbol)

    name of the start field we want to filter

  • end_sym (Symbol)

    name of the end field we want to filter

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :floor (Comparable)

    the range beginning we want to filter the data item by

  • :ceiling (Comparable)

    the range end we want to filter the data item by

  • :nil_default (Comparable)

    the value to use if the data item has no field value



154
155
156
157
158
# File 'lib/data_filter/filter_set.rb', line 154

def range_overlap_filter(start_sym, end_sym, opts = {})
  if opts[:floor] || opts[:ceiling]
    @filters << RangeOverlapFilter.new(start_sym, end_sym, **opts)
  end
end

#respond_to_missing?(sym, include_all = false) ⇒ Boolean

Used to support the DSL. Calls out to the parent scope if we receive a message we can’t respond to

Returns:

  • (Boolean)


183
184
185
# File 'lib/data_filter/filter_set.rb', line 183

def respond_to_missing?(sym, include_all = false)
  @original_self.respond_to?(sym, include_all)
end

#truthy_filter(field_sym, match: nil) ⇒ Object

Adds a TruthyFilter to the filter set

Parameters:

  • field_sym (Symbol)

    name of the field to match on

  • match (Object) (defaults to: nil)

    truthy/falsey value to use to determine whether the filter should match/filter truthy fields or falsey fields



165
166
167
168
169
170
171
172
173
# File 'lib/data_filter/filter_set.rb', line 165

def truthy_filter(field_sym, match: nil)
  # Skip filter if match is not specified
  return if match.nil?
  if is_falsey?(match)
    @filters << TruthyFilter.new(field_sym, invert: true)
  else
    @filters << TruthyFilter.new(field_sym)
  end
end