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
-
#db ⇒ Object
Returns the value of attribute db.
- #label_column ⇒ Object
-
#relationships ⇒ Object
Returns the value of attribute relationships.
-
#schema ⇒ Object
Returns the value of attribute schema.
- #slug_column ⇒ Object
- #sorting_order ⇒ Object
Instance Method Summary collapse
- #collection ⇒ Object
- #count(opts = {}) ⇒ Object
- #delete(id) ⇒ Object
- #find(selector = {}, opts = {}) ⇒ Object
- #find_one(spec_or_object_id = nil, opts = {}) ⇒ Object
- #foreign_key_name(plural = false) ⇒ Object
-
#get(id, opts = {}) ⇒ Object
CRUD.
- #human_name ⇒ Object
- #human_plural_name ⇒ Object
- #is_unique(doc = {}) ⇒ Object
- #ref(id) ⇒ Object
- #sort(id_list) ⇒ Object
Instance Attribute Details
#db ⇒ Object
Returns the value of attribute db.
15 16 17 |
# File 'lib/mongo_mutation.rb', line 15 def db @db end |
#label_column ⇒ Object
19 |
# File 'lib/mongo_mutation.rb', line 19 def label_column; @label_column ||= LABEL_COLUMNS.find{|c| @schema.keys.include?(c)||c=='_id'}; end |
#relationships ⇒ Object
Returns the value of attribute relationships.
15 16 17 |
# File 'lib/mongo_mutation.rb', line 15 def relationships @relationships end |
#schema ⇒ Object
Returns the value of attribute schema.
15 16 17 |
# File 'lib/mongo_mutation.rb', line 15 def schema @schema end |
#slug_column ⇒ Object
20 |
# File 'lib/mongo_mutation.rb', line 20 def slug_column; @slug_column ||= (@schema.find{|k,v| v[:type]==:slug}||[])[0]; end |
#sorting_order ⇒ Object
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
#collection ⇒ Object
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_name ⇒ Object
22 |
# File 'lib/mongo_mutation.rb', line 22 def human_name; self.name.gsub(/([A-Z])/, ' \1')[1..-1]; end |
#human_plural_name ⇒ Object
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 |