class XeroRuby::Where

Constants

STRING_FUNCTIONS
UUID_REGEXP

Attributes

where_opts[R]

Public Class Methods

new(where_opts) click to toggle source
# File lib/xero-ruby/where.rb, line 10
def initialize(where_opts)
  @where_opts = where_opts
end

Public Instance Methods

to_param() click to toggle source
# File lib/xero-ruby/where.rb, line 14
def to_param
  where_opts.map { |key, value| parameterize_option(key, value) }.join(' AND ')
end

Private Instance Methods

parameterize_option(key, value) click to toggle source
# File lib/xero-ruby/where.rb, line 20
def parameterize_option(key, value)
  quoted_key = quote_key(key)

  case value
  when Array
    operator, query = value

    if STRING_FUNCTIONS.include?(camelize_key(operator))
      "#{quoted_key}.#{camelize_key(operator)}(#{quote_value(query)})"
    else
    "#{quoted_key} #{operator} #{quote_value(query)}"
    end
  when Range
    "#{quoted_key} >= #{quote_value(value.first)} AND #{quoted_key} <= #{quote_value(value.last)}"
  when /^\./
    "#{quoted_key}#{value}"
  else
    "#{quoted_key} #{value}"
  end
end
quote_key(key) click to toggle source
# File lib/xero-ruby/where.rb, line 41
def quote_key(key)
  case key
  when :contact_id
    'Contact.ContactID'
  when :contact_number
    'Contact.ContactNumber'
  else
    camelize_key(key)
  end
end
quote_value(value) click to toggle source
# File lib/xero-ruby/where.rb, line 52
def quote_value(value)
  case value
  when DateTime, Date, Time
    "DateTime(#{value.strftime("%Y,%m,%d")})"
  when Numeric, false, true
    value.to_s
  when UUID_REGEXP
    %{guid("#{value}")}
  else
    %{"#{value.to_s.gsub('"', '""')}"}
  end
end