Module: MongoMutation::MutateClass

Defined in:
lib/mongo_mutation.rb

Constant Summary collapse

LABEL_COLUMNS =
['title', 'label', 'fullname', 'full_name', 'surname', 'lastname', 'last_name', 'name', 'firstname', 'first_name', 'login', 'caption', 'reference', 'file_name', 'body', '_id']

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dbObject

Returns the value of attribute db.



15
16
17
# File 'lib/mongo_mutation.rb', line 15

def db
  @db
end

#label_columnObject



19
# File 'lib/mongo_mutation.rb', line 19

def label_column; @label_column ||= LABEL_COLUMNS.find{|c| @schema.keys.include?(c)||c=='_id'}; end

#relationshipsObject

Returns the value of attribute relationships.



15
16
17
# File 'lib/mongo_mutation.rb', line 15

def relationships
  @relationships
end

#schemaObject

Returns the value of attribute schema.



15
16
17
# File 'lib/mongo_mutation.rb', line 15

def schema
  @schema
end

#slug_columnObject



20
# File 'lib/mongo_mutation.rb', line 20

def slug_column; @slug_column ||= (@schema.find{|k,v| v[:type]==:slug}||[])[0]; end

#sorting_orderObject



46
47
48
49
50
51
52
53
54
# File 'lib/mongo_mutation.rb', line 46

def sorting_order
	@sorting_order ||= if @schema.key?('position')&&!@schema['position'][:scope].nil?
		[[@schema['position'][:scope], :asc], ['position', :asc]]
	elsif @schema.key?('position')
		[['position', :asc],['_id', :asc]]
	else
		['_id', :asc]
	end
end

Instance Method Details

#collectionObject



24
# File 'lib/mongo_mutation.rb', line 24

def collection; db[self.name]; end

#count(opts = {}) ⇒ Object



44
# File 'lib/mongo_mutation.rb', line 44

def count(opts={}); collection.count(opts); end

#delete(id) ⇒ Object



64
# File 'lib/mongo_mutation.rb', line 64

def delete(id); collection.remove(ref(id)); end

#find(selector = {}, opts = {}) ⇒ Object



33
34
35
36
37
# File 'lib/mongo_mutation.rb', line 33

def find(selector={},opts={})
selector.update(opts.delete(:selector)||{})
  opts = {:sort=>self.sorting_order}.update(opts)
  collection.find(selector,opts).extend(CursorMutation)
end

#find_one(spec_or_object_id = nil, opts = {}) ⇒ Object



38
39
40
41
42
43
# File 'lib/mongo_mutation.rb', line 38

def find_one(spec_or_object_id=nil,opts={})
  spec_or_object_id.nil? ? spec_or_object_id = opts.delete(:selector) : spec_or_object_id.update(opts.delete(:selector)||{})
  opts = {:sort=>self.sorting_order}.update(opts)
  item = collection.find_one(spec_or_object_id,opts)
  item.nil? ? nil : self.new(item)
end

#foreign_key_name(plural = false) ⇒ Object



21
# File 'lib/mongo_mutation.rb', line 21

def foreign_key_name(plural=false); "id#{'s' if plural}_"+self.name; end

#get(id, opts = {}) ⇒ Object

CRUD



63
# File 'lib/mongo_mutation.rb', line 63

def get(id, opts={}); doc = collection.find_one(ref(id), opts); doc.nil? ? nil : self.new(doc); end

#human_nameObject



22
# File 'lib/mongo_mutation.rb', line 22

def human_name; self.name.gsub(/([A-Z])/, ' \1')[1..-1]; end

#human_plural_nameObject



23
# File 'lib/mongo_mutation.rb', line 23

def human_plural_name; human_name+'s'; end

#is_unique(doc = {}) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/mongo_mutation.rb', line 66

def is_unique(doc={})
  return unless collection.count==0
  doc = {'_id'=>BSON::ObjectId('000000000000000000000000')}.update(doc)
  d = self.new
	d.doc.update(doc)
	d.save 
end

#ref(id) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/mongo_mutation.rb', line 25

def ref(id)
  if id.is_a?(String)&&BSON::ObjectId.legal?(id)
	  id = BSON::ObjectId.from_string(id)
	elsif !id.is_a?(BSON::ObjectId)
	  id = ''
	end
	{'_id'=>id}
end

#sort(id_list) ⇒ Object



56
57
58
59
60
# File 'lib/mongo_mutation.rb', line 56

def sort(id_list)
  id_list.each_with_index do |id, position|
	  collection.update(ref(id), {'$set' => {'position'=>position}})
	end
end