Class: Mongoo::ModifierBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoo/modifiers.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts, doc) ⇒ ModifierBuilder

Returns a new instance of ModifierBuilder.



3
4
5
6
7
8
# File 'lib/mongoo/modifiers.rb', line 3

def initialize(opts, doc)
  @opts  = opts
  @doc   = doc
  @queue = {}
  @key_prefix = opts[:key_prefix] || ""
end

Instance Method Details

#add_to_set(k, v) ⇒ Object



61
62
63
64
# File 'lib/mongoo/modifiers.rb', line 61

def add_to_set(k,v)
  @queue["$addToSet"] ||= {}
  @queue["$addToSet"]["#{@key_prefix}#{k}"] = cast_value(v)
end

#cast_value(v) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/mongoo/modifiers.rb', line 24

def cast_value(v)
  if v.is_a?(Mongoo::Embedded::Base)
    return v.to_hash
  elsif v.is_a?(Array)
    v.collect { |e| e.is_a?(Mongoo::Embedded::Base) ? e.to_hash : e }
  else
    v
  end
end

#inc(k, v = 1) ⇒ Object



34
35
36
37
38
# File 'lib/mongoo/modifiers.rb', line 34

def inc(k, v=1)
  v = sanitize_value(k,v)
  @queue["$inc"] ||= {}
  @queue["$inc"]["#{@key_prefix}#{k}"] = cast_value(v)
end

#known_attribute?(k) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/mongoo/modifiers.rb', line 10

def known_attribute?(k)
  @doc.known_attribute?("#{@key_prefix}#{k}")
end

#pop(k) ⇒ Object



66
67
68
69
# File 'lib/mongoo/modifiers.rb', line 66

def pop(k)
  @queue["$pop"] ||= {}
  @queue["$pop"]["#{@key_prefix}#{k}"] = 1
end

#pull(k, v) ⇒ Object



71
72
73
74
# File 'lib/mongoo/modifiers.rb', line 71

def pull(k, v)
  @queue["$pull"] ||= {}
  @queue["$pull"]["#{@key_prefix}#{k}"] = cast_value(v)
end

#pull_all(k, v) ⇒ Object



76
77
78
79
# File 'lib/mongoo/modifiers.rb', line 76

def pull_all(k, v)
  @queue["$pullAll"] ||= {}
  @queue["$pullAll"]["#{@key_prefix}#{k}"] = cast_value(v)
end

#push(k, v) ⇒ Object



51
52
53
54
# File 'lib/mongoo/modifiers.rb', line 51

def push(k, v)
  @queue["$push"] ||= {}
  @queue["$push"]["#{@key_prefix}#{k}"] = cast_value(v)
end

#push_all(k, v) ⇒ Object



56
57
58
59
# File 'lib/mongoo/modifiers.rb', line 56

def push_all(k, v)
  @queue["$pushAll"] ||= {}
  @queue["$pushAll"]["#{@key_prefix}#{k}"] = cast_value(v)
end

#run!Object



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/mongoo/modifiers.rb', line 81

def run!
  if @queue.blank?
    raise ModifierUpdateError, "modifier update queue is empty"
  end

  update_query = { "_id" => @doc.id }
  update_query.merge!(@opts[:q]) if @opts[:q]

  if @opts[:only_if_current] == true
    @queue.each do |op, op_queue|
      op_queue.each do |k,v|
        update_query[k] = @doc.persisted_mongohash.dot_get(k)
      end
    end
    @opts[:update_opts] ||= {}
    @opts[:update_opts][:safe] = true
  end

  if @opts[:find_and_modify]
    ret = @doc.collection.find_and_modify(query: update_query,
                                          update: @queue,
                                          new: true)
    @doc.reload(ret)
  else
    update_opts = @opts.delete(:update_opts) || {}
    ret = @doc.collection.update(update_query, @queue, update_opts)
    if !ret.is_a?(Hash) || (ret["err"] == nil && ret["n"] == 1)
      @queue.each do |op, op_queue|
        op_queue.each do |k, v|
          duped_v = Marshal.load(Marshal.dump(v))
          case op
          when "$inc" then
            new_val = @doc.persisted_mongohash.dot_get(k).to_i + v
            @doc.mongohash.dot_set( k, new_val )
            @doc.persisted_mongohash.dot_set( k, new_val )
          when "$set" then
            @doc.mongohash.dot_set( k, v )
            @doc.persisted_mongohash.dot_set( k, duped_v )
          when "$unset" then
            @doc.mongohash.dot_delete( k )
            @doc.persisted_mongohash.dot_delete( k )
          when "$push" then
            unless @doc.persisted_mongohash.dot_get(k)
              @doc.persisted_mongohash.dot_set(k, [])
            end
            unless @doc.mongohash.dot_get(k)
              @doc.mongohash.dot_set(k, [])
            end

            @doc.persisted_mongohash.dot_get(k) << duped_v
            @doc.mongohash.dot_get(k) << v
          when "$pushAll" then
            unless @doc.persisted_mongohash.dot_get(k)
              @doc.persisted_mongohash.dot_set(k, [])
            end
            unless @doc.mongohash.dot_get(k)
              @doc.mongohash.dot_set(k, [])
            end

            @doc.persisted_mongohash.dot_get(k).concat(duped_v)
            @doc.mongohash.dot_get(k).concat(v)
          when "$addToSet" then
            unless @doc.persisted_mongohash.dot_get(k)
              @doc.persisted_mongohash.dot_set(k, [])
            end
            unless @doc.mongohash.dot_get(k)
              @doc.mongohash.dot_set(k, [])
            end

            unless @doc.persisted_mongohash.dot_get(k).include?(v)
              @doc.persisted_mongohash.dot_get(k) << duped_v
            end
            unless @doc.mongohash.dot_get(k).include?(v)
              @doc.mongohash.dot_get(k) << v
            end
          when "$pop" then
           unless @doc.persisted_mongohash.dot_get(k)
              @doc.persisted_mongohash.dot_set(k, [])
            end
            unless @doc.mongohash.dot_get(k)
              @doc.mongohash.dot_set(k, [])
            end

            @doc.persisted_mongohash.dot_get(k).pop
            @doc.mongohash.dot_get(k).pop
          when "$pull" then
            unless @doc.persisted_mongohash.dot_get(k)
              @doc.persisted_mongohash.dot_set(k, [])
            end
            unless @doc.mongohash.dot_get(k)
              @doc.mongohash.dot_set(k, [])
            end

            @doc.persisted_mongohash.dot_get(k).delete(v)
            @doc.mongohash.dot_get(k).delete(v)
          when "$pullAll" then
            unless @doc.persisted_mongohash.dot_get(k)
              @doc.persisted_mongohash.dot_set(k, [])
            end
            unless @doc.mongohash.dot_get(k)
              @doc.mongohash.dot_set(k, [])
            end

            v.each do |val|
              @doc.persisted_mongohash.dot_get(k).delete(val)
              @doc.mongohash.dot_get(k).delete(val)
            end
          end
        end
      end # @queue.each
      true
    else
      raise ModifierUpdateError, ret.inspect
    end
  end # if opts[:find_any_modify]
rescue Mongo::OperationFailure => e
  if e.message.to_s =~ /^11000\:/
    raise Mongoo::DuplicateKeyError, e.message
  else
    raise e
  end
end

#sanitize_value(k, v) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/mongoo/modifiers.rb', line 14

def sanitize_value(k,v)
  k = "#{@key_prefix}#{k}"
  if known_attribute?(k)
    field_type = @doc.class.attributes[k][:type]
    Mongoo::AttributeSanitizer.sanitize(field_type, v)
  else
    v
  end
end

#set(k, v) ⇒ Object



40
41
42
43
44
# File 'lib/mongoo/modifiers.rb', line 40

def set(k,v)
  v = sanitize_value(k,v)
  @queue["$set"] ||= {}
  @queue["$set"]["#{@key_prefix}#{k}"] = cast_value(v)
end

#unset(k) ⇒ Object



46
47
48
49
# File 'lib/mongoo/modifiers.rb', line 46

def unset(k)
  @queue["$unset"] ||= {}
  @queue["$unset"]["#{@key_prefix}#{k}"] = 1
end