class Logman::QueryBuilder

Attributes

con[RW]

Public Class Methods

new(con) click to toggle source
# File lib/console/lib/query_builder.rb, line 9
def initialize(con)
  @con = con
end

Public Instance Methods

execute(param) click to toggle source
# File lib/console/lib/query_builder.rb, line 14
def execute(param)
  str = URI.decode(param)
  obj = JSON.parse(str)
  
  obj.each do |q|
    build_concern(q)  
  end
  
  @con
end

Private Instance Methods

build_concern(q) click to toggle source
# File lib/console/lib/query_builder.rb, line 27
def build_concern(q)
  return if q['property'].blank? || q['operator'].blank?
  q['property'] = '_id' if q['property'] == 'id' 
  
  
  @con = @con.where(q['property'].to_sym => q['value']) if q['operator'] == '='
  @con = @con.where(q['property'].to_sym => Regexp.compile(".*#{q['value']}.*")) if q['operator'] == '~'
  
  
  operator_map = {
    '<'=> "$lt",
    '<='=> "$lte",
    '>'=> "$gt",
    '>='=> "$gte"
  }
  
  oper = operator_map[q['operator']]
  if oper
    @con = @con.selector[q['property']] = {oper => q['value']}
  end
  
end