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.
-
#operators ⇒ Object
readonly
Returns the value of attribute operators.
Instance Method Summary collapse
- #add_matches(field, new_matches, options) ⇒ Object
-
#initialize ⇒ QueryBuilder
constructor
A new instance of QueryBuilder.
- #to_query ⇒ Object
Constructor Details
#initialize ⇒ QueryBuilder
Returns a new instance of QueryBuilder.
11 12 13 14 |
# File 'lib/stretchy/builders/query_builder.rb', line 11 def initialize @matches = Hash.new { [] } @operators = Hash.new { 'and' } 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 |
#operators ⇒ Object (readonly)
Returns the value of attribute operators.
9 10 11 |
# File 'lib/stretchy/builders/query_builder.rb', line 9 def operators @operators end |
Instance Method Details
#add_matches(field, new_matches, options) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/stretchy/builders/query_builder.rb', line 16 def add_matches(field, new_matches, ) @matches[field] += Array(new_matches).map(&:to_s).map(&:strip).reject(&:empty?) if [:operator] @operators[field] = [:operator] elsif [:or] @operators[field] = 'or' end end |
#to_query ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/stretchy/builders/query_builder.rb', line 25 def to_query matches.map do |field, matches_for_field| Queries::MatchQuery.new( field: field, string: matches_for_field.join(' '), operator: operators[field] ) end end |