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)
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#keyword_filter(field_syms, opts = {}) ⇒ Object
Adds a KeywordFilter to the filter set.
-
#like_filter(field_sym, opts = {}) ⇒ Object
Adds a LikeFilter to the filter set.
-
#method_missing(sym, *args, &block) ⇒ Object
Used to support the DSL.
-
#prefix_filter(field_sym, opts = {}) ⇒ Object
Adds a PrefixFilter to the filter set.
-
#range_filter(field_sym, opts = {}) ⇒ Object
Adds a RangeFilter to the filter set.
-
#range_overlap_filter(start_sym, end_sym, opts = {}) ⇒ Object
Adds a RangeOverlapFilter to the filter set.
-
#respond_to_missing?(sym, include_all = false) ⇒ Boolean
Used to support the DSL.
-
#truthy_filter(field_sym, match: nil) ⇒ Object
Adds a TruthyFilter to the filter set.
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
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
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
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
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
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
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
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 |