Class: GridFSRackDAV::GridFSModel

Inherits:
Object
  • Object
show all
Includes:
GridFS, Mongo
Defined in:
lib/gridfs-rackdav/gridfs_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options)
  @connection = options[:connection]
  @collection = @connection.collection('fs.files')
  self.root = options[:root]
  self.path = resource_path
  @options = options
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



6
7
8
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 6

def collection
  @collection
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 6

def options
  @options
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 6

def path
  @path
end

#rootObject

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

#childrenObject



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'], options)
    end
  end
end

#children_namesObject



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

Returns:

  • (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

#deleteObject



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_contentsObject



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

#itemObject



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

#nameObject



35
36
37
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 35

def name
  item.nil? ? path : item['filename']
end

#path_without_rootObject



25
26
27
# File 'lib/gridfs-rackdav/gridfs_model.rb', line 25

def path_without_root
  @path[root.length..-1]
end

#saveObject



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