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


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

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

#ensure_valid_field!(k) ⇒ Object


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

def ensure_valid_field!(k)
  unless @doc.known_attribute?("#{@key_prefix}#{k}")
    raise UnknownAttributeError, "#{@key_prefix}#{k}"
  end
end

#inc(k, v = 1) ⇒ Object


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

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

#pop(k) ⇒ Object


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

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

#pull(k, v) ⇒ Object


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

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

#pull_all(k, v) ⇒ Object


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

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

#push(k, v) ⇒ Object


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

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

#push_all(k, v) ⇒ Object


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

def push_all(k, v)
  ensure_valid_field!(k)
  @queue["$pushAll"] ||= {}
  @queue["$pushAll"]["#{@key_prefix}#{k}"] = 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
# File 'lib/mongoo/modifiers.rb', line 81

def run!
  if @queue.blank?
    raise ModifierUpdateError, "modifier update queue is empty"
  end
  ret = @doc.collection.update({"_id" => @doc.id}, @queue, @opts)
  if !ret.is_a?(Hash) || (ret["err"] == nil && ret["n"] == 1)
    @queue.each do |op, op_queue|
      op_queue.each do |k, 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, v )
        when "$unset" then
          @doc.mongohash.dot_delete( k )
          @doc.persisted_mongohash.dot_delete( k )
        when "$push" then
          new_val = (@doc.persisted_mongohash.dot_get(k) || []) + [v]
          @doc.mongohash.dot_set( k, new_val )
          @doc.persisted_mongohash.dot_set( k, new_val )
        when "$pushAll" then
          new_val = (@doc.persisted_mongohash.dot_get(k) || []) + v
          @doc.mongohash.dot_set( k, new_val )
          @doc.persisted_mongohash.dot_set( k, new_val )
        when "$addToSet" then
          new_val = (@doc.persisted_mongohash.dot_get(k) || [])
          new_val << v unless new_val.include?(v)
          @doc.mongohash.dot_set(k, new_val)
          @doc.persisted_mongohash.dot_set(k, new_val)
        when "$pop" then
          new_val = (@doc.persisted_mongohash.dot_get(k) || [])
          new_val.pop
          @doc.mongohash.dot_set(k, new_val)
          @doc.persisted_mongohash.dot_set(k, new_val)
        when "$pull" then
          new_val = (@doc.persisted_mongohash.dot_get(k) || [])
          new_val.delete(v)
          @doc.mongohash.dot_set(k, new_val)
          @doc.persisted_mongohash.dot_set(k, new_val)
        when "$pullAll" then
          new_val = (@doc.persisted_mongohash.dot_get(k) || [])
          v.each do |val|
            new_val.delete(val)
          end
          @doc.mongohash.dot_set(k, new_val)
          @doc.persisted_mongohash.dot_set(k, new_val)
        end
      end
    end
    true
  else
    raise ModifierUpdateError, ret.inspect
  end
end

#sanitize_value(k, v) ⇒ Object


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

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

#set(k, v) ⇒ Object


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

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

#unset(k) ⇒ Object


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

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