Class: Crm::Core::SearchConfigurator
- Inherits:
-
Object
- Object
- Crm::Core::SearchConfigurator
- Includes:
- Enumerable
- Defined in:
- lib/crm/core/search_configurator.rb
Overview
SearchConfigurator
provides methods to incrementally configure a search request using chainable methods and to perform this search.
Chainable methods collapse
-
#add_filter(field, condition, value = nil) ⇒ SearchConfigurator
(also: #and)
Returns a new SearchConfigurator constructed by combining this configuration and the new filter.
-
#add_negated_filter(field, condition, value = nil) ⇒ SearchConfigurator
(also: #and_not)
Returns a new SearchConfigurator constructed by combining this configuration and the new negated filter.
-
#asc ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the ascending sort order.
-
#desc ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the descending sort order.
-
#limit(new_limit) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given limit.
-
#offset(new_offset) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given offset.
-
#query(new_query) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given query.
-
#sort_by(new_sort_by) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given sort criterion.
-
#sort_order(new_sort_order) ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration with the given sort order.
-
#unlimited ⇒ SearchConfigurator
Returns a new SearchConfigurator constructed by combining this configuration without limiting the number of search results.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterates over the search results.
- #first(n = :undefined) ⇒ Object
-
#initialize(settings = {}) ⇒ SearchConfigurator
constructor
A new instance of SearchConfigurator.
-
#perform_search ⇒ ItemEnumerator
Executes the search request based on this configuration.
- #take(n) ⇒ Object
-
#total ⇒ Fixnum
Returns the total number of items that match this search configuration.
Constructor Details
#initialize(settings = {}) ⇒ SearchConfigurator
Returns a new instance of SearchConfigurator.
37 38 39 40 41 |
# File 'lib/crm/core/search_configurator.rb', line 37 def initialize(settings = {}) @settings = { filters: [], }.merge(settings) end |
Instance Method Details
#add_filter(field, condition, value = nil) ⇒ SearchConfigurator Also known as: and
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration and the new filter.
Supported conditions:
-
contains_word_prefixes
-field
contains words starting withvalue
. -
contains_words
-field
contains the words given byvalue
. -
equals
-field
exactly corresponds tovalue
(case insensitive). -
is_blank
-field
is blank (omitvalue
). -
is_earlier_than
- date timefield
is earlier thanvalue
. -
is_later_than
- date timefield
is later thanvalue
. -
is_true
-field
is true (omitvalue
).
75 76 77 78 |
# File 'lib/crm/core/search_configurator.rb', line 75 def add_filter(field, condition, value = nil) new_filter = Array(@settings[:filters]) + [{field: field, condition: condition, value: value}] SearchConfigurator.new(@settings.merge(filters: new_filter)) end |
#add_negated_filter(field, condition, value = nil) ⇒ SearchConfigurator Also known as: and_not
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration and the new negated filter.
All filters (and their conditions) passed to #add_filter can be negated.
90 91 92 93 |
# File 'lib/crm/core/search_configurator.rb', line 90 def add_negated_filter(field, condition, value = nil) negated_condition = "not_#{condition}" add_filter(field, negated_condition, value) end |
#asc ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the ascending sort order.
155 156 157 |
# File 'lib/crm/core/search_configurator.rb', line 155 def asc sort_order('asc') end |
#desc ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the descending sort order.
163 164 165 |
# File 'lib/crm/core/search_configurator.rb', line 163 def desc sort_order('desc') end |
#each(&block) ⇒ Object
Iterates over the search results. Implicitly triggers #perform_search and caches its result. See ItemEnumerator#each for details.
173 174 175 176 177 |
# File 'lib/crm/core/search_configurator.rb', line 173 def each(&block) return enum_for(:each) unless block_given? perform_search.each(&block) end |
#first(n = :undefined) ⇒ Object
183 184 185 186 |
# File 'lib/crm/core/search_configurator.rb', line 183 def first(n = :undefined) return take(n) unless n == :undefined limit(1).perform_search.first end |
#limit(new_limit) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given limit.
110 111 112 |
# File 'lib/crm/core/search_configurator.rb', line 110 def limit(new_limit) SearchConfigurator.new(@settings.merge(limit: new_limit)) end |
#offset(new_offset) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given offset.
127 128 129 |
# File 'lib/crm/core/search_configurator.rb', line 127 def offset(new_offset) SearchConfigurator.new(@settings.merge(offset: new_offset)) end |
#perform_search ⇒ ItemEnumerator
Executes the search request based on this configuration.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/crm/core/search_configurator.rb', line 46 def perform_search @perform_search ||= Crm.search( filters: @settings[:filters], query: @settings[:query], limit: @settings[:limit], offset: @settings[:offset], sort_by: @settings[:sort_by], sort_order: @settings[:sort_order], ) end |
#query(new_query) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given query.
101 102 103 |
# File 'lib/crm/core/search_configurator.rb', line 101 def query(new_query) SearchConfigurator.new(@settings.merge(query: new_query)) end |
#sort_by(new_sort_by) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given sort criterion.
137 138 139 |
# File 'lib/crm/core/search_configurator.rb', line 137 def sort_by(new_sort_by) SearchConfigurator.new(@settings.merge(sort_by: new_sort_by)) end |
#sort_order(new_sort_order) ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration with the given sort order.
147 148 149 |
# File 'lib/crm/core/search_configurator.rb', line 147 def sort_order(new_sort_order) SearchConfigurator.new(@settings.merge(sort_order: new_sort_order)) end |
#take(n) ⇒ Object
179 180 181 |
# File 'lib/crm/core/search_configurator.rb', line 179 def take(n) limit(n).perform_search.to_a end |
#total ⇒ Fixnum
Returns the total number of items that match this search configuration. It can be greater than limit
. Implicitly triggers #perform_search and caches its result.
193 194 195 |
# File 'lib/crm/core/search_configurator.rb', line 193 def total perform_search.total end |
#unlimited ⇒ SearchConfigurator
Returns a new Crm::Core::SearchConfigurator constructed by combining this configuration without limiting the number of search results.
118 119 120 |
# File 'lib/crm/core/search_configurator.rb', line 118 def unlimited limit(:none) end |