Class: TinyMongo::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/tinymongo/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Model

Returns a new instance of Model.



171
172
173
174
175
176
177
178
179
# File 'lib/tinymongo/model.rb', line 171

def initialize(hash={})
  @_tinymongo_hash = {}
  defaults = self.class.instance_variable_get(:@_tinymongo_defaults)
  defaults = self.class.instance_variable_set(:@_tinymongo_defaults, {}) if(defaults.nil?)
  hash = Helper.deep_copy(defaults).merge(Helper.stringify_keys_in_hash(hash || {}))
  hash.each_pair { |key, value| set_using_setter(key, value) }
  set_tinymongo_model_class_name_in_hash(@_tinymongo_hash)
  self
end

Class Method Details

.collectionObject



33
34
35
36
37
38
39
40
41
# File 'lib/tinymongo/model.rb', line 33

def collection
  if @_tinymongo_collection_name
    TinyMongo.db[@_tinymongo_collection_name]
  elsif(defined?(Rails))
    TinyMongo.db[self.to_s.gsub('/','_').tableize]
  else
    TinyMongo.db[self.to_s]
  end
end

.countObject



111
112
113
# File 'lib/tinymongo/model.rb', line 111

def count
  collection.count
end

.create(hash = {}) ⇒ Object



82
83
84
85
# File 'lib/tinymongo/model.rb', line 82

def create(hash={})
  obj = self.new(hash)
  obj.save
end

.dbObject



29
30
31
# File 'lib/tinymongo/model.rb', line 29

def db
  TinyMongo.db
end

.delete(id) ⇒ Object



87
88
89
# File 'lib/tinymongo/model.rb', line 87

def delete(id)
  collection.remove({ '_id' => Helper.bson_object_id(id)})
end

.delete_allObject



103
104
105
# File 'lib/tinymongo/model.rb', line 103

def delete_all
  drop
end

.destroy(*args) ⇒ Object



91
92
93
# File 'lib/tinymongo/model.rb', line 91

def destroy(*args)
  delete(*args)
end

.destroy_allObject



107
108
109
# File 'lib/tinymongo/model.rb', line 107

def destroy_all
  drop
end

.distinct(key, query = nil) ⇒ Object



163
164
165
166
167
168
# File 'lib/tinymongo/model.rb', line 163

def distinct(key, query=nil)
  if(query.kind_of? Hash)
    query = Helper.hashify_models_in(query)
  end
  collection.distinct(key, query)
end

.dropObject



99
100
101
# File 'lib/tinymongo/model.rb', line 99

def drop
  collection.drop
end

.drop_index(obj) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/tinymongo/model.rb', line 129

def drop_index(obj)
  if(obj.instance_of? String)
    index = obj
  elsif(obj.kind_of? Hash)
    index = get_index_name(obj)
  elsif(obj.kind_of? Array)
    index = get_index_name(Hash[obj])
  else
    return
  end
  
  collection.drop_index(index)
end

.drop_indexesObject



147
148
149
# File 'lib/tinymongo/model.rb', line 147

def drop_indexes
  collection.drop_indexes
end

.dropIndex(*args) ⇒ Object



143
144
145
# File 'lib/tinymongo/model.rb', line 143

def dropIndex(*args)
  drop_index(*args)
end

.dropIndexesObject



151
152
153
# File 'lib/tinymongo/model.rb', line 151

def dropIndexes
  drop_indexes
end

.ensure_index(keys, options = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/tinymongo/model.rb', line 115

def ensure_index(keys, options={})
  if(keys.kind_of? Hash)
    keys = keys.map { |k,v| [k.to_s, convert_ascending_descending_2d(v)]}
  elsif(keys.kind_of? Array)
    keys = keys.map { |o| (o.kind_of? Array) ? [o[0].to_s, convert_ascending_descending_2d(o[1])] : nil }.compact
  end
  options = Helper.symbolify_keys_in_hash(options)
  collection.create_index(keys, options)
end

.ensureIndex(*args) ⇒ Object



125
126
127
# File 'lib/tinymongo/model.rb', line 125

def ensureIndex(*args)
  ensure_index(*args)
end

.find(query = {}, fields = nil, limit = nil, skip = nil) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/tinymongo/model.rb', line 55

def find(query={}, fields=nil, limit=nil, skip=nil)
  query = Helper.hashify_models_in(query)
  fields = Helper.hashify_models_in(fields)
  
  add_tinymongo_model_class_name_key_to_fields(fields)
  Cursor.new(collection.find(query, {:fields => fields, :limit => limit, :skip => skip}), self)
end

.find_one(query = {}, fields = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tinymongo/model.rb', line 63

def find_one(query={}, fields=nil)
  return nil unless query
  
  if([BSON::ObjectID, String].include? query.class)
    query = {'_id' => Helper.bson_object_id(query)}
  else
    query = Helper.hashify_models_in(query)
    fields = Helper.hashify_models_in(fields)
  end
  
  add_tinymongo_model_class_name_key_to_fields(fields)
  hash = collection.find_one(query, {:fields => fields})
  hash ? Helper.deserialize_hashes_in(hash) : nil
end

.findOne(*args) ⇒ Object



78
79
80
# File 'lib/tinymongo/model.rb', line 78

def findOne(*args)
  find_one(*args)
end

.full_nameObject



43
44
45
# File 'lib/tinymongo/model.rb', line 43

def full_name
  "#{db.name}.#{collection.name}"
end

.get_full_nameObject



47
48
49
# File 'lib/tinymongo/model.rb', line 47

def get_full_name
  full_name
end

.get_indexesObject



155
156
157
# File 'lib/tinymongo/model.rb', line 155

def get_indexes
  collection.index_information.map { |k,v| v }
end

.getFullNameObject



51
52
53
# File 'lib/tinymongo/model.rb', line 51

def getFullName
  full_name
end

.getIndexesObject



159
160
161
# File 'lib/tinymongo/model.rb', line 159

def getIndexes
  get_indexes
end

.mongo_collection(name) ⇒ Object



25
26
27
# File 'lib/tinymongo/model.rb', line 25

def mongo_collection(name)
  @_tinymongo_collection_name = name.to_s
end

.mongo_key(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tinymongo/model.rb', line 4

def mongo_key(*args)
  default = nil
  @_tinymongo_defaults = {} if @_tinymongo_defaults.nil?
  
  args.each do |arg|
    default = arg[:default] || arg['default'] if(arg.kind_of? Hash)
  end
  
  args.each do |arg|
    if([Symbol, String].include? arg.class)
      key_name_s = arg.to_s
      key_name_sym = arg.to_sym

      define_method(key_name_sym) { instance_variable_get(:@_tinymongo_hash)[key_name_s] }
      define_method("#{key_name_s}=") { |val| instance_variable_get(:@_tinymongo_hash)[key_name_s] = val }
      
      @_tinymongo_defaults[key_name_s] = default if default
    end
  end
end

.remove(*args) ⇒ Object



95
96
97
# File 'lib/tinymongo/model.rb', line 95

def remove(*args)
  delete(*args)
end

Instance Method Details

#==(another) ⇒ Object



189
190
191
192
# File 'lib/tinymongo/model.rb', line 189

def ==(another)
  (self.instance_variable_get(:@_tinymongo_hash) == another.instance_variable_get(:@_tinymongo_hash)) &&
  (self.kind_of? TinyMongo::Model) && (another.kind_of? TinyMongo::Model)
end

#_idObject



181
182
183
# File 'lib/tinymongo/model.rb', line 181

def _id
  @_tinymongo_hash['_id']
end

#_id=(val) ⇒ Object



185
186
187
# File 'lib/tinymongo/model.rb', line 185

def _id=(val)
  @_tinymongo_hash['_id'] = Helper.bson_object_id(val)
end

#add_to_set(hash = {}) ⇒ Object



275
276
277
# File 'lib/tinymongo/model.rb', line 275

def add_to_set(hash={})
  do_modifier_operation_and_reload('$addToSet', hash)
end

#addToSet(*args) ⇒ Object



279
280
281
# File 'lib/tinymongo/model.rb', line 279

def addToSet(*args)
  add_to_set(*args)
end

#collectionObject



198
199
200
# File 'lib/tinymongo/model.rb', line 198

def collection
  self.class.collection
end

#dbObject



194
195
196
# File 'lib/tinymongo/model.rb', line 194

def db
  self.db
end

#deleteObject



241
242
243
244
245
# File 'lib/tinymongo/model.rb', line 241

def delete
  if(@_tinymongo_hash['_id'])
    collection.remove({ '_id' => @_tinymongo_hash['_id'] })
  end
end

#destroyObject



247
248
249
# File 'lib/tinymongo/model.rb', line 247

def destroy
  delete
end

#inc(hash = {}) ⇒ Object



251
252
253
# File 'lib/tinymongo/model.rb', line 251

def inc(hash={})
  do_modifier_operation_and_reload('$inc', hash)
end

#pop(hash = {}) ⇒ Object



283
284
285
# File 'lib/tinymongo/model.rb', line 283

def pop(hash={})
  do_modifier_operation_and_reload('$pop', hash)
end

#pull(hash = {}) ⇒ Object



287
288
289
# File 'lib/tinymongo/model.rb', line 287

def pull(hash={})
  do_modifier_operation_and_reload('$pull', hash)
end

#pull_all(hash = {}) ⇒ Object



291
292
293
# File 'lib/tinymongo/model.rb', line 291

def pull_all(hash={})
  do_modifier_operation_and_reload('$pullAll', hash)
end

#pullAll(*args) ⇒ Object



295
296
297
# File 'lib/tinymongo/model.rb', line 295

def pullAll(*args)
  pull_all(*args)
end

#push(hash = {}) ⇒ Object



263
264
265
# File 'lib/tinymongo/model.rb', line 263

def push(hash={})
  do_modifier_operation_and_reload('$push', hash)
end

#push_all(hash = {}) ⇒ Object



267
268
269
# File 'lib/tinymongo/model.rb', line 267

def push_all(hash={})
  do_modifier_operation_and_reload('$pushAll', hash)
end

#pushAll(*args) ⇒ Object



271
272
273
# File 'lib/tinymongo/model.rb', line 271

def pushAll(*args)
  push_all(*args)
end

#reloadObject



210
211
212
213
214
215
# File 'lib/tinymongo/model.rb', line 210

def reload
  if(@_tinymongo_hash['_id'])
    obj = collection.find_one({ '_id' => @_tinymongo_hash['_id'] })
    @_tinymongo_hash = Hash[obj.map { |k,v| [k.to_s, Helper.deserialize_hashes_in(v)] }] if(obj)
  end
end

#saveObject



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/tinymongo/model.rb', line 217

def save
  if(@_tinymongo_hash['_id'].nil?) # new 
    oid = collection.save(Helper.hashify_models_in(@_tinymongo_hash))
    if(oid)
      @_tinymongo_hash.delete(:_id)
      @_tinymongo_hash['_id'] = oid
    end
  else # update
    collection.update({ '_id' => @_tinymongo_hash['_id'] }, Helper.hashify_models_in(@_tinymongo_hash), :upsert => true)
    reload
  end
  return self
end

#set(hash = {}) ⇒ Object



255
256
257
# File 'lib/tinymongo/model.rb', line 255

def set(hash={})
  do_modifier_operation_and_reload('$set', hash)
end

#to_hashObject



202
203
204
# File 'lib/tinymongo/model.rb', line 202

def to_hash
  hash = @_tinymongo_hash.dup
end

#to_paramObject



206
207
208
# File 'lib/tinymongo/model.rb', line 206

def to_param
  @_tinymongo_hash['_id'].to_s
end

#unset(hash = {}) ⇒ Object



259
260
261
# File 'lib/tinymongo/model.rb', line 259

def unset(hash={})
  do_modifier_operation_and_reload('$unset', hash)
end

#update_attribute(key, value) ⇒ Object



231
232
233
234
# File 'lib/tinymongo/model.rb', line 231

def update_attribute(key, value)
  set_using_setter(key, value)
  save
end

#update_attributes(hash = {}) ⇒ Object



236
237
238
239
# File 'lib/tinymongo/model.rb', line 236

def update_attributes(hash={})
  hash.each_pair { |key, value| set_using_setter(key, value) }
  save
end