Class: Stretchy::Builders::QueryBuilder
- Inherits:
-
Object
- Object
- Stretchy::Builders::QueryBuilder
- Extended by:
- Forwardable
- Defined in:
- lib/stretchy/builders/query_builder.rb
Instance Attribute Summary collapse
-
#matches ⇒ Object
readonly
Returns the value of attribute matches.
-
#queries ⇒ Object
readonly
Returns the value of attribute queries.
-
#query_opts ⇒ Object
readonly
Returns the value of attribute query_opts.
Instance Method Summary collapse
- #add_matches(field, new_matches, options = {}) ⇒ Object
- #add_query(query) ⇒ Object
-
#initialize ⇒ QueryBuilder
constructor
A new instance of QueryBuilder.
- #to_enum ⇒ Object
- #to_queries ⇒ Object
Constructor Details
#initialize ⇒ QueryBuilder
Returns a new instance of QueryBuilder.
11 12 13 14 15 |
# File 'lib/stretchy/builders/query_builder.rb', line 11 def initialize @queries = [] @matches = Hash.new { [] } @query_opts = Hash.new { {} } end |
Instance Attribute Details
#matches ⇒ Object (readonly)
Returns the value of attribute matches.
9 10 11 |
# File 'lib/stretchy/builders/query_builder.rb', line 9 def matches @matches end |
#queries ⇒ Object (readonly)
Returns the value of attribute queries.
9 10 11 |
# File 'lib/stretchy/builders/query_builder.rb', line 9 def queries @queries end |
#query_opts ⇒ Object (readonly)
Returns the value of attribute query_opts.
9 10 11 |
# File 'lib/stretchy/builders/query_builder.rb', line 9 def query_opts @query_opts end |
Instance Method Details
#add_matches(field, new_matches, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/stretchy/builders/query_builder.rb', line 21 def add_matches(field, new_matches, = {}) @matches[field] += Array(new_matches) opts = {} [:operator, :slop, :minimum_should_match, :min, :type].each do |opt| opts[opt] = [opt] if [opt] end @query_opts[field] = opts end |
#add_query(query) ⇒ Object
17 18 19 |
# File 'lib/stretchy/builders/query_builder.rb', line 17 def add_query(query) @queries << query end |
#to_enum ⇒ Object
30 31 32 |
# File 'lib/stretchy/builders/query_builder.rb', line 30 def to_enum queries + matches.values end |
#to_queries ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/stretchy/builders/query_builder.rb', line 34 def to_queries queries + matches.map do |field, phrases| opts = query_opts[field].merge( field: field, string: phrases.flatten.join(' ') ) Queries::MatchQuery.new(opts) end end |