lib/ddr/index/query_builder.rb in ddr-models-2.4.0.rc5 vs lib/ddr/index/query_builder.rb in ddr-models-2.4.0.rc6

- old
+ new

@@ -10,51 +10,61 @@ # 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. + # 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. + # 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 [{field => order, ...}], ... # Adds ordering(s) to the query. + # # Aliased as: sort # # present [field] # Adds a filter selecting document where the field has any value. # @@ -73,12 +83,12 @@ # term [{field => value, ...}] # Adds a filter of "term" query clauses for the fields and values. # # where [{field => 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 + # 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. # class QueryBuilder # Builds a Query object @@ -93,12 +103,12 @@ builder.query end attr_reader :query - def initialize(&block) - @query = Query.new + def initialize(query = nil, &block) + @query = query || Query.new if block_given? instance_eval &block end end @@ -118,10 +128,10 @@ alias_method :filters, :filter # @param fields [Array<Field>] # @return [QueryBuilder] self def field(*fields) - query.fields += fields + query.fields += fields.flatten.map { |f| FieldAttribute.coerce(f) } self end alias_method :fields, :field # @param num [Integer]