Class: Quickbooks::Util::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/quickbooks/util/query_builder.rb

Constant Summary collapse

VALUE_QUOTE =
"'"

Instance Method Summary collapse

Constructor Details

#initializeQueryBuilder

Returns a new instance of QueryBuilder.



9
10
# File 'lib/quickbooks/util/query_builder.rb', line 9

def initialize
end

Instance Method Details

#clause(field, operator, value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/quickbooks/util/query_builder.rb', line 12

def clause(field, operator, value)
  # replace with an escaped backslash
  escape_single_quotes = -> field { field.to_s.gsub("'", "\\\\'") }

  value = case value
          when DateTime, Time
            value.iso8601
          when Date
            value.strftime('%Y-%m-%d')
          when Array
            value = value.map(&escape_single_quotes)
          else
            value = escape_single_quotes.call(value)
          end

  if operator.downcase == 'in' && value.is_a?(Array)
    value = value.map { |v| "#{VALUE_QUOTE}#{v}#{VALUE_QUOTE}" }
    "#{field} #{operator} (#{value.join(', ')})"
  else
    "#{field} #{operator} #{VALUE_QUOTE}#{value}#{VALUE_QUOTE}"
  end
end