Class: MongoGrid
- Inherits:
-
Object
- Object
- MongoGrid
- Defined in:
- lib/models/mongo_grid.rb
Instance Method Summary collapse
- #delete_media(key) ⇒ Object
- #get_id(key) ⇒ Object
- #get_media(key) ⇒ Object
-
#initialize(grid, meta_data_collection) ⇒ MongoGrid
constructor
A new instance of MongoGrid.
- #media_links ⇒ Object
- #save_media(media, file_name = nil) ⇒ Object
Constructor Details
#initialize(grid, meta_data_collection) ⇒ MongoGrid
Returns a new instance of MongoGrid.
2 3 4 5 |
# File 'lib/models/mongo_grid.rb', line 2 def initialize grid, @grid = grid @meta_data_collection = end |
Instance Method Details
#delete_media(key) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/models/mongo_grid.rb', line 23 def delete_media key existing_id = get_id key if (existing_id) @grid.delete(existing_id) delete_map = media_map delete_map['media'].delete(key) save_media_map delete_map end end |
#get_id(key) ⇒ Object
42 43 44 |
# File 'lib/models/mongo_grid.rb', line 42 def get_id key media_map['media'][key] end |
#get_media(key) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/models/mongo_grid.rb', line 46 def get_media key id = get_id key begin file = @grid.get id rescue end if (file != nil) media = MongoMedia.new media.body = file.read media.content_type= file.content_type if(file.) media.last_updated= file.['_koda_last_modified'] media else media end end end |
#media_links ⇒ Object
36 37 38 39 40 |
# File 'lib/models/mongo_grid.rb', line 36 def media_links media_map.raw_document['media'].keys.map do |name| {'href' => '/content/media/' + name.to_s, 'rel' => 'full', 'title' => name.to_s} end end |
#save_media(media, file_name = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/models/mongo_grid.rb', line 7 def save_media media, file_name=nil map = media_map delete_media file_name id = @grid.put(media.body, {:content_type => media.content_type, :metadata => {'_koda_last_modified' => media.last_updated}.to_s }) if (!file_name) file_name = id.to_s end map['media'][file_name] = id save_media_map map file_name end |