Class: MongoDatabase

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

Instance Method Summary collapse

Constructor Details

#initialize(database) ⇒ MongoDatabase

Returns a new instance of MongoDatabase.



4
5
6
# File 'lib/models/mongo_database.rb', line 4

def initialize database
 @db = database
end

Instance Method Details

#all_user_collections(user, isadmin = false) ⇒ Object



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

def all_user_collections(user,isadmin=false)
  user_collections = resource_collections.clone
  return user_collections if(isadmin)
  user_collections.each do |collection|
	  access_control = collection(collection).find_document('access-control')
	  if(access_control && access_control['read_users'] != "*" && !(access_control['read_users'].include? user))
	    user_collections.delete collection
   end
 end
 user_collections
end

#collection(collection_name) ⇒ Object



37
38
39
# File 'lib/models/mongo_database.rb', line 37

def collection collection_name
  MongoCollection.new @db.collection(collection_name)
end


12
13
14
15
16
# File 'lib/models/mongo_database.rb', line 12

def collection_links(user)
  all_user_collections(user.alias,user.isadmin).map do |collection|
    {'href' => '/api/' + collection, 'rel' => 'full', 'title' => collection, 'alias' => collection}
  end
end

#contains_collection(collection_name) ⇒ Object



52
53
54
# File 'lib/models/mongo_database.rb', line 52

def contains_collection collection_name
  resource_collections.include?(collection_name)  
end


18
19
20
21
22
23
# File 'lib/models/mongo_database.rb', line 18

def content_collection_links
  collections = all_user_collections('*',false).clone
  collections.map do |collection|
    { 'href' => '/content/' + collection.to_s, 'title' => collection.to_s }
  end
end

#filter(collection_name, search_hash, take, skip) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/models/mongo_database.rb', line 118

def filter(collection_name, search_hash, take, skip)
  
  if(search_hash["sort"] != nil)
    sort_hash = search_hash["sort"]
  end

  search_hash["filter"].each do |k,v|
    if(v.include? '/')
      search_hash["filter"][k] = eval v
    else
      search_hash["filter"][k] = v
    end
  end

  collection(collection_name).query(search_hash["filter"], take, skip, sort_hash)
  
end

#flat_fileObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/models/mongo_database.rb', line 56

def flat_file
  flat_file = []
    all_user_collections('*').each do |collection|

      if(collection != 'objectlabs-system' && collection != '_koda_media') 
        docs = collection(collection).content_links(nil, nil, nil)
        docs_in_collection = []
        docs.each do |doc|
          doc_from_db = collection(collection).find_document(doc['alias'])
          if(doc_from_db)
            docs_in_collection.push(doc_from_db.stripped_document)
          end
        end
        flat_file.push({'collection'=>collection, 'docs' => docs_in_collection})
      end
    end
  flat_file
end

#resource_collection_urlsObject



8
9
10
# File 'lib/models/mongo_database.rb', line 8

def resource_collection_urls 
  resource_collections.map {|e| '/' + e }	    
end

#resource_collectionsObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/models/mongo_database.rb', line 41

def resource_collections
  collections = @db.collection_names
	collections.delete 'system.indexes'  
	collections.delete 'system.users'  
	collections.delete '_koda_meta'
	collections.delete 'fs.chunks'  
	collections.delete 'fs.files'
	  	
  collections
end

#search(params, collection_name = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/models/mongo_database.rb', line 75

def search(params, collection_name=nil)
  
  search_hash = Hash.new
  sort_hash = Hash.new

  if(params && params.length > 0)
    params.each do |k,v|
      if(v.include? '/')
        search_hash[k] = eval v
      else
        search_hash[k] = v
      end
      if (v == 'true' || v == 'false')
        search_hash[k] = v == 'true'
      end
      sort_hash[k] = 1
    end

    results = Hash.new
    
    if(collection_name)
      search_hash.delete 'collection'
      search_hash.delete 'splat'
      search_hash.delete 'captures'

      return collection(collection_name).query(search_hash, params[:take], params[:skip], sort_hash)
    end

    all_user_collections('*').each do |collection|
      collection_result = collection(collection).query(search_hash, params[:take], params[:skip], sort_hash)
      if(collection_result && collection_result.length > 0)
        results[collection] = collection_result
      end
    end
    
    results

  else
    flat_file
  end
  
end