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.



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

def initialize(hash={})
  @_tinymongo_hash = (self.class.instance_variable_get(:@_tinymongo_defaults).merge(Helper.stringify_keys_in_hash(hash)) || {}) if hash
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



85
86
87
# File 'lib/tinymongo/model.rb', line 85

def count
  collection.count
end

.create(hash = {}) ⇒ Object



60
61
62
63
# File 'lib/tinymongo/model.rb', line 60

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



65
66
67
# File 'lib/tinymongo/model.rb', line 65

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

.delete_allObject



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

def delete_all
  drop
end

.destroy(id) ⇒ Object



69
70
71
# File 'lib/tinymongo/model.rb', line 69

def destroy(id)
  delete(id)
end

.destroy_allObject



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

def destroy_all
  drop
end

.dropObject



73
74
75
# File 'lib/tinymongo/model.rb', line 73

def drop
  collection.drop
end

.find(*args) ⇒ Object



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

def find(*args)
  new_args = args.map {|arg| Helper.hashify_models_in(arg) }
  Cursor.new(collection.find(*new_args), self)
end

.find_one(*args) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tinymongo/model.rb', line 48

def find_one(*args)
  if((args.length > 0) && (args.compact.length == 0))
    return nil
  elsif((args.length == 1) && ([BSON::ObjectID, String].include? args[0].class))
    hash = collection.find_one({'_id' => Helper.bson_object_id(args[0])})
  else
    new_args = args.map {|arg| Helper.hashify_models_in(arg) }
    hash = collection.find_one(*new_args)
  end
  hash ? self.new(hash) : nil
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.instance_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

Instance Method Details

#==(another) ⇒ Object



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

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



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

def _id
  @_tinymongo_hash['_id']
end

#_id=(val) ⇒ Object



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

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

#add_to_set(hash = {}) ⇒ Object



192
193
194
# File 'lib/tinymongo/model.rb', line 192

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

#collectionObject



119
120
121
# File 'lib/tinymongo/model.rb', line 119

def collection
  self.class.collection
end

#dbObject



115
116
117
# File 'lib/tinymongo/model.rb', line 115

def db
  self.db
end

#deleteObject



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

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

#destroyObject



168
169
170
# File 'lib/tinymongo/model.rb', line 168

def destroy
  delete
end

#idObject



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

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

#id=(val) ⇒ Object



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

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

#inc(hash = {}) ⇒ Object



172
173
174
# File 'lib/tinymongo/model.rb', line 172

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

#pop(hash = {}) ⇒ Object



196
197
198
# File 'lib/tinymongo/model.rb', line 196

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

#pull(hash = {}) ⇒ Object



200
201
202
# File 'lib/tinymongo/model.rb', line 200

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

#pull_all(hash = {}) ⇒ Object



204
205
206
# File 'lib/tinymongo/model.rb', line 204

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

#push(hash = {}) ⇒ Object



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

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

#push_all(hash = {}) ⇒ Object



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

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

#reloadObject



131
132
133
134
135
136
# File 'lib/tinymongo/model.rb', line 131

def reload
  if(@_tinymongo_hash['_id'])
    obj = collection.find_one({ '_id' => @_tinymongo_hash['_id'] })
    @_tinymongo_hash = Helper.stringify_keys_in_hash(obj) if(obj)
  end
end

#saveObject



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tinymongo/model.rb', line 138

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

#set(hash = {}) ⇒ Object



176
177
178
# File 'lib/tinymongo/model.rb', line 176

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

#to_hashObject



123
124
125
# File 'lib/tinymongo/model.rb', line 123

def to_hash
  @_tinymongo_hash.dup
end

#to_paramObject



127
128
129
# File 'lib/tinymongo/model.rb', line 127

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

#unset(hash = {}) ⇒ Object



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

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

#update_attribute(name, value) ⇒ Object



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

def update_attribute(name, value)
  send(name.to_s + '=', value)
  save
end

#update_attributes(hash = {}) ⇒ Object



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

def update_attributes(hash={})
  hash.each_pair { |key, value| send(key.to_s + '=', value) }
  save
end