Class: AdvancedQueryBuilder
- Inherits:
-
Object
- Object
- AdvancedQueryBuilder
- Defined in:
- common/advanced_query_builder.rb
Defined Under Namespace
Classes: RangeValue
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Class Method Summary collapse
Instance Method Summary collapse
-
#and(field_or_subquery, value = nil, type = 'text', literal = false, negated = false) ⇒ Object
-
#build ⇒ Object
-
#empty? ⇒ Boolean
(also: #empty)
-
#initialize ⇒ AdvancedQueryBuilder
constructor
A new instance of AdvancedQueryBuilder.
-
#or(field_or_subquery, value = nil, type = 'text', literal = false, negated = false) ⇒ Object
Constructor Details
#initialize ⇒ AdvancedQueryBuilder
Returns a new instance of AdvancedQueryBuilder.
9 10 11 |
# File 'common/advanced_query_builder.rb', line 9 def initialize @query = nil end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query
5 6 7 |
# File 'common/advanced_query_builder.rb', line 5 def query @query end |
Class Method Details
.build_query_from_form(queries) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'common/advanced_query_builder.rb', line 59 def self.build_query_from_form(queries) query = if queries.length > 1 stack = queries.reverse.clone while stack.length > 1 a = stack.pop b = stack.pop stack.push(JSONModel::JSONModel(:boolean_query).from_hash({ :op => b["op"], :subqueries => [as_field_query(a), as_field_query(b)] })) end stack.pop else as_field_query(queries[0]) end JSONModel::JSONModel(:advanced_query).from_hash({"query" => query}) end |
.from_json_filter_terms(array_of_json) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'common/advanced_query_builder.rb', line 48 def self.from_json_filter_terms(array_of_json) builder = new array_of_json.each do |json_str| json = ASUtils.json_parse(json_str) builder.and(json.keys[0], json.values[0]) end builder.build end |
Instance Method Details
#and(field_or_subquery, value = nil, type = 'text', literal = false, negated = false) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'common/advanced_query_builder.rb', line 13 def and(field_or_subquery, value = nil, type = 'text', literal = false, negated = false) if field_or_subquery.is_a?(AdvancedQueryBuilder) push_subquery('AND', field_or_subquery) elsif value.is_a? RangeValue push_range('AND', field_or_subquery, value, 'range', literal, negated) else raise "Missing value" if value.nil? push_term('AND', field_or_subquery, value, type, literal, negated) end self end |
#build ⇒ Object
44 45 46 |
# File 'common/advanced_query_builder.rb', line 44 def build JSONModel::JSONModel(:advanced_query).from_hash({"query" => build_query(@query)}) end |
#empty? ⇒ Boolean Also known as: empty
39 40 41 |
# File 'common/advanced_query_builder.rb', line 39 def empty? @query.nil? end |
#or(field_or_subquery, value = nil, type = 'text', literal = false, negated = false) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'common/advanced_query_builder.rb', line 26 def or(field_or_subquery, value = nil, type = 'text', literal = false, negated = false) if field_or_subquery.is_a?(AdvancedQueryBuilder) push_subquery('OR', field_or_subquery) elsif value.is_a? RangeValue push_range('AND', field_or_subquery, value, 'range', literal, negated) else raise "Missing value" unless value push_term('OR', field_or_subquery, value, type, literal, negated) end self end |