Module: Mongorilla::Document

Defined in:
lib/mongorilla/document.rb

Constant Summary collapse

RELOAD =
:reload
SYNC =
:sync
ASYNC =
:async

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convertObject


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mongorilla/document.rb', line 43

def self.convert
  lambda{|m|
    case m
    when Time
      m.localtime
    when String
      m.dup
    when Symbol
      m.to_s
    else
      m
    end
  }
end

.convert_stringObject


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongorilla/document.rb', line 58

def self.convert_string
  lambda{|m|
    case m
    when String
      m.dup
    when Symbol
      m.to_s
    when BSON::ObjectId
      m.to_s
    else
      m
    end
  }
end

.included(c) ⇒ Object


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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/mongorilla/document.rb', line 74

def self.included(c)
  fields = c.const_get("#{c}Fields")
  fields.each do |f|
    define_method(f) {@doc[f.to_s] }
    define_method(f.to_s + "="){|v| set(f.to_s,v);}
  end
  alias_method :id,:_id
  col_name = pluralize(c.to_s)
  c.instance_variable_set("@col",Collection.new(col_name))

  def initialize(doc)
    @orig = doc
    @doc = Document.recursive_convert(@orig,Document.convert)
    @changes={}
  end

  def [](f)
    send(f.to_sym)
  end

  def attributes
    fields = self.class.const_get("#{self.class}Fields")
    {}.tap{|h| fields.each{|f| h[f.to_s] = @doc[f.to_s]}}
  end

  def to_hash
    Document.recursive_convert(attributes,Document.convert_string)
  end

  def to_json
    to_hash.to_json
  end

  def to_yaml
    to_hash.to_yaml
  end

  def inspect
    to_hash.inspect
  end

  def to_s
    inspect
  end

  def origin
    @orig
  end

  def changes
    @changes
  end

  def set(f,v)
    send(f)
    @doc[f.to_s] = v
    @changes["$set"] ||= {}
    @changes["$set"][f.to_s] = v
  end

  alias []= set

  def inc(f,v)
    @doc[f] = 0 unless send(f)
    @doc[f] += v
    @changes["$inc"] ||= {}
    @changes["$inc"][f] = v
  end

  def push(f,v)
    @doc[f] = [] unless send(f)
    @doc[f].push(v)
    @changes["$push"] ||= {}
    @changes["$push"][f] = v
  end

  def unset(f)
    @doc.delete(f)
    @changes["$unset"] ||= {}
    @changes["$unset"][f] = 1
  end

  def push_all(f,v)
    if send(f)
      @doc[f] += v
    else
      @doc[f] = v
    end
    @changes["$pushAll"] ||= {}
    @changes["$pushAll"][f] = v
  end

  def add_to_set(f,v)
    if v.is_a? Hash
      values = v["$each"]
    else
      values = [v]
    end
    values.each do|val|
      if send(f)
        @doc[f].push(val) unless @doc[f].find{|r| r == val}
      else
        @doc[f] = [val]
      end
    end
    @changes["$addToSet"] ||= {}
    @changes["$addToSet"][f] = v
  end

  def pop(f,v)
    @changes["$pop"] ||= {}
    @changes["$pop"][f] = v
    if v > 0
      send(f).pop
    else
      send(f).shift
    end
  end

  def pull(f,v)
    @changes["$pull"] ||= {}
    @changes["$pull"][f] = v
  end

  def pull_all(f,v)
    @changes["$pull_all"] ||= {}
    @changes["$pull_all"][f] = v
  end

  def save(*args)
    cond = {}
    opt = {}
    mode = SYNC
    args.each do|arg|
      if arg.is_a?(Symbol)
        mode = arg
      elsif cond == {} 
        cond = arg
      else
        opt = arg
      end
    end
    opt[:safe] = true if [SYNC,RELOAD].include?(mode)
    cond.merge!({"_id" => @doc["_id"]})
    if @changes.keys.length == 0
      Collection.output_log("warn","save no changed data cond:#{cond.inspect} orig:#{@orig.inspect} doc:#{@doc.inspect}")
      reset
      return false
    end
    ret = self.class.collection.update(cond,@changes,opt)
    if opt[:safe] && ret["n"] != 1
      reset
      return false
    end
    if mode == RELOAD
      reload
      return true
    end
    @orig = Marshal.load(Marshal.dump(@doc))
    @changes={}
    true
  end

  def reset
    @changes={}
    @doc = Marshal.load(Marshal.dump(@orig))
  end

  def delete
    self.class.remove(:_id => id)
  end

  def reload
    @changes={}
    @orig = self.class.collection.find_one(id,:master => true)
    @doc = Marshal.load(Marshal.dump(@orig))
  end

  def c.collection
    @col
  end

  def c.create(data,opt={})
    if opt == {}
      opt[:safe] = true
    end
    ret = @col.insert(data,opt)
    if opt[:safe] == true
      if ret.is_a? Array
        find({:_id => {"$in" => ret}},:master=>true)
      elsif ret
        find(ret,:master=>true)
      end
    end
  end

  def c.find_one(cond,opt={})
    ret = @col.find_one(cond,opt)
    if ret
      return self.new(ret)
    else
      return nil
    end
  end

  def c.count(cond={},opt={})
    @col.count(cond,opt)
  end

  def c.find(cond={},opt={})
    if !cond.is_a? Hash
      find_one(cond,opt)
    else
      ret = @col.find(cond,opt)
      Cursor.new(self,ret,@col,cond,opt)
    end
  end

  def c.update(cond,data,opt={})
    @col.update(cond,data,opt)
  end

  def c.remove(cond={},opt={})
    @col.remove(cond,opt)
  end
end

.pluralize(name) ⇒ Object


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mongorilla/document.rb', line 8

def self.pluralize(name)
  underscore_name = name.gsub(/^[A-Z]/){|m| "_" + m.downcase}[1 .. -1]
  case underscore_name
  when /(s|ss|sh|ch|o|x)$/
    underscore_name + "es"
  when /[^aeiou]y$/
    underscore_name[0 .. -2] + "ies"
  when /(f|fe)$/
    underscore_name.gsub(/(f|fe)$/){|m| "ves"}
  when /child$/
    underscore_name + "en"
  when /foot$/
    underscore_name.gsub(/foot$/){|m| "feet"}
  when /tooth$/
    underscore_name.gsub(/tooth$/){|m| "teeth"}
  when /man$/
    underscore_name.gsub(/man$/){|m| "men"}
  when /woman$/
    underscore_name.gsub(/woman$/){|m| "women"}
  else
    underscore_name + "s"
  end
end

.recursive_convert(m, trancefar) ⇒ Object


32
33
34
35
36
37
38
39
40
41
# File 'lib/mongorilla/document.rb', line 32

def self.recursive_convert(m,trancefar)
  case m
  when Array
    m.map{|a| recursive_convert(a,trancefar)}
  when Hash
    {}.tap{|h| m.each{|k,v| h[k.to_s] = recursive_convert(v,trancefar)}}
  else
    trancefar.call(m)
  end
end

Instance Method Details

#[](f) ⇒ Object


90
91
92
# File 'lib/mongorilla/document.rb', line 90

def [](f)
  send(f.to_sym)
end

#add_to_set(f, v) ⇒ Object


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/mongorilla/document.rb', line 166

def add_to_set(f,v)
  if v.is_a? Hash
    values = v["$each"]
  else
    values = [v]
  end
  values.each do|val|
    if send(f)
      @doc[f].push(val) unless @doc[f].find{|r| r == val}
    else
      @doc[f] = [val]
    end
  end
  @changes["$addToSet"] ||= {}
  @changes["$addToSet"][f] = v
end

#attributesObject


94
95
96
97
# File 'lib/mongorilla/document.rb', line 94

def attributes
  fields = self.class.const_get("#{self.class}Fields")
  {}.tap{|h| fields.each{|f| h[f.to_s] = @doc[f.to_s]}}
end

#changesObject


123
124
125
# File 'lib/mongorilla/document.rb', line 123

def changes
  @changes
end

#deleteObject


242
243
244
# File 'lib/mongorilla/document.rb', line 242

def delete
  self.class.remove(:_id => id)
end

#inc(f, v) ⇒ Object


136
137
138
139
140
141
# File 'lib/mongorilla/document.rb', line 136

def inc(f,v)
  @doc[f] = 0 unless send(f)
  @doc[f] += v
  @changes["$inc"] ||= {}
  @changes["$inc"][f] = v
end

#initialize(doc) ⇒ Object


84
85
86
87
88
# File 'lib/mongorilla/document.rb', line 84

def initialize(doc)
  @orig = doc
  @doc = Document.recursive_convert(@orig,Document.convert)
  @changes={}
end

#inspectObject


111
112
113
# File 'lib/mongorilla/document.rb', line 111

def inspect
  to_hash.inspect
end

#originObject


119
120
121
# File 'lib/mongorilla/document.rb', line 119

def origin
  @orig
end

#pop(f, v) ⇒ Object


183
184
185
186
187
188
189
190
191
# File 'lib/mongorilla/document.rb', line 183

def pop(f,v)
  @changes["$pop"] ||= {}
  @changes["$pop"][f] = v
  if v > 0
    send(f).pop
  else
    send(f).shift
  end
end

#pull(f, v) ⇒ Object


193
194
195
196
# File 'lib/mongorilla/document.rb', line 193

def pull(f,v)
  @changes["$pull"] ||= {}
  @changes["$pull"][f] = v
end

#pull_all(f, v) ⇒ Object


198
199
200
201
# File 'lib/mongorilla/document.rb', line 198

def pull_all(f,v)
  @changes["$pull_all"] ||= {}
  @changes["$pull_all"][f] = v
end

#push(f, v) ⇒ Object


143
144
145
146
147
148
# File 'lib/mongorilla/document.rb', line 143

def push(f,v)
  @doc[f] = [] unless send(f)
  @doc[f].push(v)
  @changes["$push"] ||= {}
  @changes["$push"][f] = v
end

#push_all(f, v) ⇒ Object


156
157
158
159
160
161
162
163
164
# File 'lib/mongorilla/document.rb', line 156

def push_all(f,v)
  if send(f)
    @doc[f] += v
  else
    @doc[f] = v
  end
  @changes["$pushAll"] ||= {}
  @changes["$pushAll"][f] = v
end

#reloadObject


246
247
248
249
250
# File 'lib/mongorilla/document.rb', line 246

def reload
  @changes={}
  @orig = self.class.collection.find_one(id,:master => true)
  @doc = Marshal.load(Marshal.dump(@orig))
end

#resetObject


237
238
239
240
# File 'lib/mongorilla/document.rb', line 237

def reset
  @changes={}
  @doc = Marshal.load(Marshal.dump(@orig))
end

#save(*args) ⇒ Object


203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/mongorilla/document.rb', line 203

def save(*args)
  cond = {}
  opt = {}
  mode = SYNC
  args.each do|arg|
    if arg.is_a?(Symbol)
      mode = arg
    elsif cond == {} 
      cond = arg
    else
      opt = arg
    end
  end
  opt[:safe] = true if [SYNC,RELOAD].include?(mode)
  cond.merge!({"_id" => @doc["_id"]})
  if @changes.keys.length == 0
    Collection.output_log("warn","save no changed data cond:#{cond.inspect} orig:#{@orig.inspect} doc:#{@doc.inspect}")
    reset
    return false
  end
  ret = self.class.collection.update(cond,@changes,opt)
  if opt[:safe] && ret["n"] != 1
    reset
    return false
  end
  if mode == RELOAD
    reload
    return true
  end
  @orig = Marshal.load(Marshal.dump(@doc))
  @changes={}
  true
end

#set(f, v) ⇒ Object


127
128
129
130
131
132
# File 'lib/mongorilla/document.rb', line 127

def set(f,v)
  send(f)
  @doc[f.to_s] = v
  @changes["$set"] ||= {}
  @changes["$set"][f.to_s] = v
end

#to_hashObject


99
100
101
# File 'lib/mongorilla/document.rb', line 99

def to_hash
  Document.recursive_convert(attributes,Document.convert_string)
end

#to_jsonObject


103
104
105
# File 'lib/mongorilla/document.rb', line 103

def to_json
  to_hash.to_json
end

#to_sObject


115
116
117
# File 'lib/mongorilla/document.rb', line 115

def to_s
  inspect
end

#to_yamlObject


107
108
109
# File 'lib/mongorilla/document.rb', line 107

def to_yaml
  to_hash.to_yaml
end

#unset(f) ⇒ Object


150
151
152
153
154
# File 'lib/mongorilla/document.rb', line 150

def unset(f)
  @doc.delete(f)
  @changes["$unset"] ||= {}
  @changes["$unset"][f] = 1
end