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
176 177 178 |
# File 'lib/data_filter/filter_set.rb', line 176 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
119 120 121 122 123 |
# File 'lib/data_filter/filter_set.rb', line 119 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
95 96 97 98 99 |
# File 'lib/data_filter/filter_set.rb', line 95 def like_filter(field_sym, opts = {}) if opts[:by] @filters << LikeFilter.new(field_sym, opts[:by]) end end |
#prefix_filter(field_sym, opts = {}) ⇒ Object
Adds a PrefixFilter to the filter set
107 108 109 110 111 |
# File 'lib/data_filter/filter_set.rb', line 107 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
135 136 137 138 139 |
# File 'lib/data_filter/filter_set.rb', line 135 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
153 154 155 156 157 |
# File 'lib/data_filter/filter_set.rb', line 153 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
182 183 184 |
# File 'lib/data_filter/filter_set.rb', line 182 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
164 165 166 167 168 169 170 171 172 |
# File 'lib/data_filter/filter_set.rb', line 164 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 |