Class: MongoRecord::Subobject

Inherits:
Base show all
Defined in:
lib/mongo_record/subobject.rb

Overview

A MongoRecord::Subobject is an MongoRecord::Base subclass that disallows many operations. Subobjects are those that are contained within and saved with some other object.

Using MongoRecord::Subobject is completely optional.

As an example, say a Student object contains an Address. You might want to make Address a subclass of Subobject so that you don’t accidentally try to save an address to a collection by itself.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #[], #[]=, all, arrays, #attributes, #attributes_from_column_definition, belongs_to, collection, connection, connection=, #create, #destroy, #eql?, field, field_names, find_by_mql, find_each, first, has_and_belongs_to_many, has_many, has_one, #hash, index, indexes, inherited, #initialize, instantiate, last, method_missing, #method_missing, mongo_ivar_names, #save, #save!, #set_create_times, subobjects, sum, #to_mongo_value, update, update_all, #update_attribute, #update_attributes, #update_attributes!, #valid?

Constructor Details

This class inherits a constructor from MongoRecord::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MongoRecord::Base

Class Method Details

.collection_name(coll_name) ⇒ Object

Subobjects ignore the collection name.



33
34
# File 'lib/mongo_record/subobject.rb', line 33

def collection_name(coll_name)
end

.count(*args) ⇒ Object

Disallow count.



42
43
44
# File 'lib/mongo_record/subobject.rb', line 42

def count(*args)
  complain("counted")
end

.create(values_hash) ⇒ Object

Disallow create.



68
69
70
# File 'lib/mongo_record/subobject.rb', line 68

def create(values_hash)
  complain("created")
end

.delete(id) ⇒ Object

Disallow delete.



47
48
49
# File 'lib/mongo_record/subobject.rb', line 47

def delete(id)
  complain("deleted")
end

.delete_all(conditions = nil) ⇒ Object

Disallow delete_all.



63
64
65
# File 'lib/mongo_record/subobject.rb', line 63

def delete_all(conditions=nil)
  complain("deleted")
end

.destroy(id) ⇒ Object

Disallow destroy.



53
54
55
# File 'lib/mongo_record/subobject.rb', line 53

def destroy(id)
  complain("destroyed")
end

.destroy_all(conditions = nil) ⇒ Object

Disallow destroy_all.



58
59
60
# File 'lib/mongo_record/subobject.rb', line 58

def destroy_all(conditions = nil)
  complain("destroyed")
end

.find(*args) ⇒ Object

Disallow find.



37
38
39
# File 'lib/mongo_record/subobject.rb', line 37

def find(*args)
  complain("found")
end

.removeObject

Disallow delete.



50
51
52
# File 'lib/mongo_record/subobject.rb', line 50

def delete(id)
  complain("deleted")
end

Instance Method Details

#deleteObject Also known as: remove

Disallow delete and remove.



102
103
104
# File 'lib/mongo_record/subobject.rb', line 102

def delete
  self.class.complain("deleted")
end

#idObject

Subobjects do not have their own ids. You’ll get a deprecation warning if you call this outside of Rails.



87
# File 'lib/mongo_record/subobject.rb', line 87

def id; raise "Subobjects don't have ids"; end

#id=(val) ⇒ Object

Subobjects do not have their own ids.



83
# File 'lib/mongo_record/subobject.rb', line 83

def id=(val); raise "Subobjects don't have ids"; end

#new_record?Boolean

Disallow new_record?

Returns:

  • (Boolean)


94
# File 'lib/mongo_record/subobject.rb', line 94

def new_record?; raise "Subobjects don't have ids"; end

#to_paramObject

to_param normally returns the id of an object. Since subobjects don’t have ids, this is disallowed.



91
# File 'lib/mongo_record/subobject.rb', line 91

def to_param; raise "Subobjects don't have ids"; end

#updateObject

Disallow udpate.



97
98
99
# File 'lib/mongo_record/subobject.rb', line 97

def update
  self.class.complain("updated")
end