Module: Lims::Core::Persistence::Sequel::Filters
- Included in:
- Persistor
- Defined in:
- lib/lims-core/persistence/sequel/filters.rb
Overview
Implementes filter methods needed by persitors.
Constant Summary collapse
- LIKE_OPERATOR =
'LIKE'
- COMPARISON_OPERATORS =
["<", "<=", "=", ">=", ">", LIKE_OPERATOR]
Instance Method Summary collapse
-
#add_comparison_filter(dataset, comparison_criteria) ⇒ Persistor
Joins the comparison filter to the existing persistor.
-
#comparison_filter(criteria) ⇒ Persistor
Implement a comparison filter for a Sequel::Persistor.
-
#multi_criteria_filter(criteria) ⇒ Persistor
Implement a multicriteria filter for a Sequel::Persistor.
Instance Method Details
#add_comparison_filter(dataset, comparison_criteria) ⇒ Persistor
Joins the comparison filter to the existing persistor.
56 57 58 59 |
# File 'lib/lims-core/persistence/sequel/filters.rb', line 56 def add_comparison_filter(dataset, comparison_criteria) comparison_persistor = comparison_filter(comparison_criteria) self.class.new(self, dataset.join(comparison_persistor.dataset, :id => :key).qualify) end |
#comparison_filter(criteria) ⇒ Persistor
Implement a comparison filter for a Sequel::Persistor. Key being the name of the resource’s field and the value is a Hash. The key of the hash is a comparison operator and the value is the given value the filter do the comparison against.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lims-core/persistence/sequel/filters.rb', line 33 def comparison_filter(criteria) clause = "" criteria.each do |field, comparison_expression| comparison_expression.each do |operator, value| if operator.upcase == LIKE_OPERATOR operator = operator.upcase value = "%#{value}%" if operator == LIKE_OPERATOR end raise ArgumentError, "Not supported comparison operator has been given: '#{operator}'" unless COMPARISON_OPERATORS.include?(operator) clause = clause + ') & (' unless clause == "" clause = clause + " #{field} " + operator + "'#{value}'" end end self.class.new(self, dataset.where(clause).qualify) end |
#multi_criteria_filter(criteria) ⇒ Persistor
Implement a multicriteria filter for a Sequel::Persistor. Value can be either a String, an Array or a Hash. Strings and Arrays are normal filters, whereas Hashes correspond to a joined search. The criteria will apply to the joined object corresponding to the key.
19 20 21 22 23 24 25 |
# File 'lib/lims-core/persistence/sequel/filters.rb', line 19 def multi_criteria_filter(criteria) # We need to create the adequat dataset. dataset = __multi_criteria_filter(criteria).dataset # As the dataset can include join, we need to select only the columns # corresponding to the persistor self.class.new(self, dataset.qualify(table_name).distinct()) end |