Module: EasyModelSelects

Defined in:
lib/easy_model_selects.rb,
lib/easy_model_selects/version.rb

Constant Summary collapse

VERSION =
"0.9.9.1"
@@modifiers =
[{">>"=> ">","<<"=> "<","=="=> "=","<="=> "<=",">="=> ">=", "$N" => "!=", "$L"=> "LIKE", "$I"=> "IN"},
{">>"=> "$gt","<<"=> "$lt","=="=> "$eq","<="=> "$lte",">="=> "$gte", "$N"=> "$ne", "$I"=> "$in", "Value splitting for Array for $in"=>"$/"}]
@@operators =

0 => sql 1 => mongodb $/ to split arrays for $I

[{"$and"=>"and","$xor"=>"or"},
{"$and"=>"$and","$xor"=>"$or"}]

Class Method Summary collapse

Class Method Details

.delete_last_and(wrong_and_at_the_end) ⇒ Object

db.inventory.where( {

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

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



141
142
143
# 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) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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 = {}) ⇒ Object

actual key funtions



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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 = {}) ⇒ Object



26
27
28
29
30
31
32
33
# 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 = {}) ⇒ Object



34
35
36
37
38
39
40
41
# 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 = {}) ⇒ Object

better gem functionality



13
14
15
16
17
18
19
20
21
22
23
24
# 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