Class: Ddr::Index::QueryBuilder
- Inherits:
-
Object
- Object
- Ddr::Index::QueryBuilder
- Defined in:
- lib/ddr/index/query_builder.rb
Overview
QueryBuilder - Provides a DSL for building a Query.
Note: Where a method receives a [field] parameter, the parameter value is coerced to a Field instance. See FieldAttribute#coerce for details.
*** DSL METHODS ***
absent [field]
Adds a filter selecting documents where the field is not present (no values).
asc [field], …
Adds ascending orderings by the fields specified.
See also: desc, order_by
before [field], [date_time]
Adds a filter selecting documents where the field has a date/time before
(earlier than) the value.
before_days [field], [int]
Adds a filter selecting documents where the field has a date/time the
specified number of days before today (now) or earlier.
desc [field], …
Adds descending orderings by the fields specified.
See also: asc, order_by
id [doc_id]
For selecting a single document by ID.
filter [filter1], …
Adds filters to the query.
Aliased as: filters
filters [filter], …
Alias for: filter
field [field1], …
Adds fields to result documents.
Note that all fields are returned when none is specified.
Aliased as: fields
fields [field], …
Alias for: field
limit [int]
Limits the number of documents returned by the query.
Aliased as: rows
model [model_name], …
Adds a filter selecting document where ActiveFedora model equals value
or one of the values.
negative [field], [value]
Adds a filter selecting document where field does not have the value.
order_by [=> order, …], …
Adds ordering(s) to the query.
Aliased as: sort
present [field]
Adds a filter selecting document where the field has any value.
q [query_clause]
Sets a query clause for the `q` parameter.
raw [clause1], …
Adds a filter of "raw" query clauses (i.e., pre-constructed).
rows [int]
Alias for: limit
sort [=> order, …]
Alias for: order_by
term [=> value, …]
Adds a filter of "term" query clauses for the fields and values.
where [=> value, …]
Adds a filter of "standard" query clauses.
Values will be escaped when the filter is serialized.
If a hash value is an array, that query clause will select documents
where the field matches any array entry.
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#asc(*fields) ⇒ QueryBuilder
Self.
-
#desc(*fields) ⇒ QueryBuilder
Self.
-
#field(*fields) ⇒ QueryBuilder
(also: #fields)
Self.
- #filter(*filters) ⇒ QueryBuilder (also: #filters)
- #id(pid) ⇒ QueryBuilder
-
#initialize(query = nil, &block) ⇒ QueryBuilder
constructor
A new instance of QueryBuilder.
-
#limit(num) ⇒ QueryBuilder
(also: #rows)
Self.
-
#order_by(*orderings) ⇒ QueryBuilder
(also: #sort)
Self.
-
#q(query_clause) ⇒ QueryBuilder
Self.
Constructor Details
#initialize(query = nil, &block) ⇒ QueryBuilder
96 97 98 99 100 101 |
# File 'lib/ddr/index/query_builder.rb', line 96 def initialize(query = nil, &block) @query = query || Query.new if block_given? instance_eval &block end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
168 169 170 171 172 173 174 |
# File 'lib/ddr/index/query_builder.rb', line 168 def method_missing(name, *args, &block) if respond_to?(name) filter Filter.send(name, *args) else super end end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
94 95 96 |
# File 'lib/ddr/index/query_builder.rb', line 94 def query @query end |
Instance Method Details
#asc(*fields) ⇒ QueryBuilder
144 145 146 147 |
# File 'lib/ddr/index/query_builder.rb', line 144 def asc(*fields) query.sort += fields.map { |field| SortOrder.asc(field) } self end |
#desc(*fields) ⇒ QueryBuilder
150 151 152 153 |
# File 'lib/ddr/index/query_builder.rb', line 150 def desc(*fields) query.sort += fields.map { |field| SortOrder.desc(field) } self end |
#field(*fields) ⇒ QueryBuilder Also known as: fields
120 121 122 123 |
# File 'lib/ddr/index/query_builder.rb', line 120 def field(*fields) query.fields += fields.flatten.map { |f| FieldAttribute.coerce(f) } self end |
#filter(*filters) ⇒ QueryBuilder Also known as: filters
112 113 114 115 |
# File 'lib/ddr/index/query_builder.rb', line 112 def filter(*filters) query.filters += filters self end |
#id(pid) ⇒ QueryBuilder
105 106 107 108 |
# File 'lib/ddr/index/query_builder.rb', line 105 def id(pid) q QueryClause.id(pid) limit 1 end |
#limit(num) ⇒ QueryBuilder Also known as: rows
128 129 130 131 |
# File 'lib/ddr/index/query_builder.rb', line 128 def limit(num) query.rows = num.to_i self end |
#order_by(*orderings) ⇒ QueryBuilder Also known as: sort
136 137 138 139 |
# File 'lib/ddr/index/query_builder.rb', line 136 def order_by(*orderings) query.sort += orderings.first.map { |field, order| SortOrder.new(field: field, order: order) } self end |
#q(query_clause) ⇒ QueryBuilder
157 158 159 160 |
# File 'lib/ddr/index/query_builder.rb', line 157 def q(query_clause) query.q = query_clause self end |