Class: MongoCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/models/mongo_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ MongoCollection

Returns a new instance of MongoCollection.



5
6
7
# File 'lib/models/mongo_collection.rb', line 5

def initialize collection
  @collection = collection
end

Instance Method Details



21
22
23
# File 'lib/models/mongo_collection.rb', line 21

def content_links take=nil, skip=nil, sort=nil
  content_links_from_docs @collection.find({ '$or'=> [{'_koda_hidden_file'=>nil},{'_koda_hidden_file'=>false}] },build_options(take, skip, sort)).map
end

#deleteObject



88
89
90
# File 'lib/models/mongo_collection.rb', line 88

def delete
  @collection.drop
end

#delete_document(ref) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/models/mongo_collection.rb', line 80

def delete_document ref
  existing_doc = find_document(ref)

  if (existing_doc)
    @collection.remove('_id' => existing_doc.id)
  end
end

#find_document(doc_ref) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/models/mongo_collection.rb', line 25

def find_document doc_ref
  doc = create_document_wrapper @collection.find_one("alias"=>doc_ref)
  if (doc == nil)
    begin
      bsonid = BSON::ObjectId.from_string doc_ref
      doc = create_document_wrapper @collection.find_one(bsonid)
    rescue
    end
  end

  doc
end

#query(query_map, take = nil, skip = nil, sort = nil) ⇒ Object



38
39
40
# File 'lib/models/mongo_collection.rb', line 38

def query query_map, take=nil, skip=nil, sort=nil
  content_links_from_docs @collection.find(query_map, build_options(take, skip, sort))
end


13
14
15
# File 'lib/models/mongo_collection.rb', line 13

def resource_links take=nil, skip=nil, sort=nil
  resource_links_from_docs @collection.find({},build_options(take, skip, sort)).map
end


17
18
19
# File 'lib/models/mongo_collection.rb', line 17

def resource_links_no_hidden take=nil, skip=nil, sort=nil
  resource_links_from_docs @collection.find({ '$or'=> [{'_koda_hidden_file'=>nil},{'_koda_hidden_file'=>false}] },build_options(take, skip, sort)).map
end

#resource_urlsObject



9
10
11
# File 'lib/models/mongo_collection.rb', line 9

def resource_urls
  @collection.find.map{|doc| (create_document_wrapper doc).url}
end

#save_document(raw_resource, ref = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/models/mongo_collection.rb', line 42

def save_document raw_resource, ref=nil

  if(raw_resource['linked_documents'] != nil)
    raw_resource.delete 'linked_documents'
  end

  if !(ref==nil)
    existing_doc = find_document(ref)
  end

  if !(existing_doc == nil)      
    updated_doc = MongoDocument.new raw_resource, @collection.name, existing_doc.id, Time.now.httpdate
    @collection.save(updated_doc.raw_document)
  else
    new_id = @collection.insert(raw_resource)
    
    if(raw_resource['_koda_indexes'] && raw_resource['_koda_indexes'] != '')
      indexes = raw_resource['_koda_indexes'].split(',')
      index_collection = []
      indexes.each do |index|
        index_collection.push [index, Mongo::ASCENDING]
      end
      @collection.ensure_index index_collection 
    end
    
    updated_doc = MongoDocument.new raw_resource, @collection.name, new_id, Time.now.httpdate

    if(ref and updated_doc.ref != ref)
      updated_doc.ref = ref
      @collection.save(raw_resource)
    end

    updated_doc.is_new = true
  end

  updated_doc
end