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.



6
7
8
9
10
11
# File 'lib/mongoo/modifiers.rb', line 6

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

Instance Method Details

#add_to_set(k, v) ⇒ Object



64
65
66
67
# File 'lib/mongoo/modifiers.rb', line 64

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

#cast_value(v) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/mongoo/modifiers.rb', line 27

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



37
38
39
40
41
# File 'lib/mongoo/modifiers.rb', line 37

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)


13
14
15
# File 'lib/mongoo/modifiers.rb', line 13

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

#pop(k) ⇒ Object



69
70
71
72
# File 'lib/mongoo/modifiers.rb', line 69

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

#pull(k, v) ⇒ Object



74
75
76
77
# File 'lib/mongoo/modifiers.rb', line 74

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

#pull_all(k, v) ⇒ Object



79
80
81
82
# File 'lib/mongoo/modifiers.rb', line 79

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

#push(k, v) ⇒ Object



54
55
56
57
# File 'lib/mongoo/modifiers.rb', line 54

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

#push_all(k, v) ⇒ Object



59
60
61
62
# File 'lib/mongoo/modifiers.rb', line 59

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

#run!Object



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
# File 'lib/mongoo/modifiers.rb', line 84

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]
end

#sanitize_value(k, v) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/mongoo/modifiers.rb', line 17

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



43
44
45
46
47
# File 'lib/mongoo/modifiers.rb', line 43

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

#unset(k) ⇒ Object



49
50
51
52
# File 'lib/mongoo/modifiers.rb', line 49

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