module EasyModelSelects

Constants

VERSION

Public Class Methods

delete_last_and(wrong_and_at_the_end) click to toggle source

db.inventory.where( {

$and : [
    { $or : [ { price : 0.99 }, { price : 1.99 } ] },
    { $or : [ { sale : true }, { qty : { $lt : 20 } } ] }
]

} ) db.inventory.find( { qty: { $in: [ 5, 15 ] } } )

# File lib/easy_model_selects.rb, line 141
def self.delete_last_and(wrong_and_at_the_end)
  no_and_at_the_end = wrong_and_at_the_end.from(0).to(wrong_and_at_the_end.length - 5)
end
get_where_condition(param_name, param_value, where_condition, modifier, operator) click to toggle source
# File lib/easy_model_selects.rb, line 119
def self.get_where_condition(param_name, param_value, where_condition, modifier, operator)
  if @@db == 0
    where_condition[0] = "#{where_condition[0]}#{param_name} #{modifier} (?) #{operator} "#question
    where_condition.push (modifier != "IN" ? "#{param_value}" : param_value.split("$/") )
  else
    where_condition["#{operator}"] = [] unless where_condition.include?("#{operator}")
    where_condition["#{operator}"] << {}
    where_condition["#{operator}"][where_condition["#{operator}"].size - 1]["#{param_name}"] = {}
    where_condition["#{operator}"][where_condition["#{operator}"].size - 1]["#{param_name}"]["#{modifier}"] = (modifier != "$in" ? "#{param_value}" : param_value.split("$/") ) 
  end
  #return
  where_condition
end
get_where_statement(param_name, param_value, where_condition) click to toggle source
# File lib/easy_model_selects.rb, line 64
def self.get_where_statement(param_name, param_value, where_condition)
    
  #care for easy writing
  param_name = "updated_at" if param_name == "modified_since"
    
  #care for operator staff
  operatorkey = "$and"
  modifier  = @@modifiers[@@db]["=="]
  if (defined? param_value.include?) == nil #fixnum and integer do not have include function
    param_value = [param_value]
  end
  
  if param_value.include? @@operators[@@db].keys.last#each element recommendet, but not necessary at the moment
    operatorkey = @@operators[@@db].keys.last
  end
  
  param_size = param_value.split(operatorkey).size
  param_value.split(operatorkey).each_with_index do |param_value_sep, index|
    if @@db == 0 and index == param_size - 1
      #operator to next statement needs to be and not or, or can just be between a condition for one attribute
      operatorkey = "$and"
    end
  
    #care for modifiers
    if @@modifiers[@@db].key? param_value_sep[0..1]
      modifier = @@modifiers[@@db][param_value_sep[0..1]]
      param_value_sep = param_value_sep[2..param_value_sep.length]
    end

    where_condition = get_where_condition(param_name, param_value_sep, where_condition, modifier, @@operators[@@db][operatorkey])
  end
    
  #care for array staff
  #if array param_value, the staff should be selected in more optimized way: (selecting every given possibility instead of just one)
  if @@db == 0
    if param_value.include? "{" and param_value.include? "}"
      array_to_be_found = param_value.clone
      array_to_be_found.remove!("{","}")

      if !where_condition[0].nil?
        where_condition[0] = delete_last_and(where_condition[0])
        where_condition[0] = "#{where_condition[0]} or "
      end

      array_to_be_found.split(",").each do |array_value|
        where_condition[0] = "#{where_condition[0]}(?) = ANY (#{param_name}) or "#question
        where_condition.push "#{array_value}" #values
      end
    end
  else
    #no mongodb array support yet
  end
  #return
  where_condition
end
get_where_statement_from_param(params, options = {}) click to toggle source

actual key funtions

# File lib/easy_model_selects.rb, line 44
def self.get_where_statement_from_param(params, options = {})
  #options handling
    if options.include?(:db)
      @@db = options[:db] == :mongodb ? 1 : 0
    else
      @@db = 0
    end
    
  #prepare where statemant out of names and values in an array
    @@db == 0 ? where_condition = [] : where_condition = {}
    params.each do |param_name, param_value|
        where_condition = get_where_statement(param_name, param_value, where_condition)
    end
    #delete last connection in sql queries
    if @@db ==  0
      where_condition[0] = delete_last_and(where_condition[0])
    end
    where_condition
end
modifiers(options = {}) click to toggle source
# File lib/easy_model_selects.rb, line 26
def self.modifiers(options = {})
  if options.include?(:db)
    db = options[:db] == :mongodb ? 1 : 0
  else
    db = 0
  end
  @@modifiers[db]
end
operators(options = {}) click to toggle source
# File lib/easy_model_selects.rb, line 34
def self.operators(options = {})
  if options.include?(:db)
    db = options[:db] == :mongodb ? 1 : 0
  else
    db = 0
  end
  @@operators[db]
end
set_configurations(options = {}) click to toggle source

better gem functionality

# File lib/easy_model_selects.rb, line 13
def self.set_configurations(options = {})
  changed = 0
  if options.include?(:db)
    if options[:db] == :mongodb
      @@db = 1
    else
      @@db = 0
    end
    changed = changed + 1
  end
  "#{changed} #{changed > 1 ? "Configurations" : "Configuration"} changed."
end