Class: GridFSRackDAV::GridFSModel
- Inherits:
-
Object
- Object
- GridFSRackDAV::GridFSModel
- Includes:
- GridFS, Mongo
- Defined in:
- lib/gridfs-rackdav/gridfs_model.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #children ⇒ Object
- #children_names ⇒ Object
- #collection? ⇒ Boolean
- #delete ⇒ Object
- #get_file_contents ⇒ Object
-
#initialize(resource_path, options) ⇒ GridFSModel
constructor
A new instance of GridFSModel.
- #item ⇒ Object
- #name ⇒ Object
- #path_without_root ⇒ Object
- #save ⇒ Object
- #write(file_contents = '') ⇒ Object
Constructor Details
#initialize(resource_path, options) ⇒ GridFSModel
Returns a new instance of GridFSModel.
8 9 10 11 12 13 14 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 8 def initialize(resource_path, ) @connection = [:connection] @collection = @connection.collection('fs.files') self.root = [:root] self.path = resource_path @options = end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
6 7 8 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 6 def collection @collection end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 6 def @options end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 6 def path @path end |
#root ⇒ Object
Returns the value of attribute root.
6 7 8 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 6 def root @root end |
Instance Method Details
#children ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 57 def children if self.collection? && !item.nil? @collection.find({:filename => /^(#{Regexp.escape(self.item['filename'])})[^\/]+(\/?)$/}).map do |c| self.class.new(c['filename'], ) end end end |
#children_names ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 46 def children_names children_names = [] if self.collection? && !item.nil? @collection.find({:filename => /^(#{Regexp.escape(self.item['filename'])})[^\/]+(\/?)$/}).each do |r| children_names << r['filename'] if r['filename'] != self.path end children_names end children_names end |
#collection? ⇒ Boolean
28 29 30 31 32 33 34 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 28 def collection? if item.nil? path[-1,1] == '/' else item['filename'][-1,1] == '/' end end |
#delete ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 85 def delete if collection? @collection.remove({:filename => /^(#{Regexp.escape(item['filename'])}).*/}) else @collection.remove({:filename => /^(#{Regexp.escape(item['filename'])})\/?$/}) end end |
#get_file_contents ⇒ Object
65 66 67 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 65 def get_file_contents GridStore.open(@connection, self.path, 'r') { |f| f.read } end |
#item ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 38 def item @item ||= @collection.find_one({:filename => /^(#{Regexp.escape(self.path)})\/?$/}) if @item.nil? and path == (self.root + '/').gsub(/\/+/, '/') @item = self.write end @item end |
#name ⇒ Object
35 36 37 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 35 def name item.nil? ? path : item['filename'] end |
#path_without_root ⇒ Object
25 26 27 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 25 def path_without_root @path[root.length..-1] end |
#save ⇒ Object
82 83 84 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 82 def save @collection.save(self.item) end |
#write(file_contents = '') ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 68 def write(file_contents = '') filename = ('/' + self.path).gsub(/\/+/, '/') GridStore.open(@connection, filename, 'w') do |f| f.content_type = MIME::Types.type_for(filename).first.to_s f.content_type = 'text/html' if f.content_type.empty? f. = { :ctime => Time.now.to_i, :mtime => Time.now.to_i } f.write(file_contents) end @item = nil item end |