Class: SQB::QueryString
- Inherits:
-
Object
- Object
- SQB::QueryString
- Defined in:
- lib/sqb/query_string.rb
Instance Method Summary collapse
- #add(*items) ⇒ Object
- #group(joiner, &block) ⇒ Object
-
#initialize(&block) ⇒ QueryString
constructor
A new instance of QueryString.
- #to_s ⇒ Object
- #with_brackets(&block) ⇒ Object
Constructor Details
#initialize(&block) ⇒ QueryString
Returns a new instance of QueryString.
4 5 6 7 |
# File 'lib/sqb/query_string.rb', line 4 def initialize(&block) @query = [] block.call(self) if block_given? end |
Instance Method Details
#add(*items) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/sqb/query_string.rb', line 13 def add(*items) if @group_joiner @group_items ||= [] @group_items << items else items.flatten.each do |item| add_to_query(resolve_for_query(item)) end end end |
#group(joiner, &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sqb/query_string.rb', line 24 def group(joiner, &block) @group_joiner = joiner block.call ensure if @group_items # If we have some group items after running our group, # add them into the query now. @group_items.each do |items| raw_q = items.map { |i| resolve_for_query(i) }.join(' ') raw_q << @group_joiner unless items === @group_items.last add_to_query(raw_q) end end @group_items = nil @group_joiner = nil end |
#to_s ⇒ Object
9 10 11 |
# File 'lib/sqb/query_string.rb', line 9 def to_s @query.join(" ") end |
#with_brackets(&block) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/sqb/query_string.rb', line 41 def with_brackets(&block) @open_bracket = true block.call ensure @query.last << ")" @open_bracket = nil @close_bracket = nil end |