Class: MongoidExt::File
Instance Attribute Summary collapse
Instance Method Summary
collapse
allocate, #assign_id, demongoize, field, #id, #initialize, mongoize
Constructor Details
This class inherits a constructor from EmbeddedHash
Instance Attribute Details
#_list_name ⇒ Object
Returns the value of attribute _list_name.
4
5
6
|
# File 'lib/mongoid_ext/file.rb', line 4
def _list_name
@_list_name
end
|
#_root_document ⇒ Object
Returns the value of attribute _root_document.
3
4
5
|
# File 'lib/mongoid_ext/file.rb', line 3
def _root_document
@_root_document
end
|
Instance Method Details
#data ⇒ Object
92
93
94
95
96
97
98
99
|
# File 'lib/mongoid_ext/file.rb', line 92
def data
if self.get
self.get.data
else
puts "WARNING: the file you are trying to read doesn't exist: #{self.inspect}"
nil
end
end
|
#delete ⇒ Object
110
111
112
113
|
# File 'lib/mongoid_ext/file.rb', line 110
def delete
@io = nil
gridfs.delete(grid_filename)
end
|
#each(&block) ⇒ Object
101
102
103
104
105
106
107
108
|
# File 'lib/mongoid_ext/file.rb', line 101
def each(&block)
if self.get
self.get.each(&block)
else
puts "WARNING: the file you are trying to read doesn't exist: #{self.inspect}"
nil
end
end
|
#get ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/mongoid_ext/file.rb', line 54
def get
@io ||= begin
io = nil
begin
io = gridfs.get(grid_filename)
def io.read
self.data
end
rescue Mongoid::Errors::DocumentNotFound => e
end
io
end
end
|
#grid_filename ⇒ Object
72
73
74
|
# File 'lib/mongoid_ext/file.rb', line 72
def grid_filename
@grid_filename ||= "#{_root_document.collection.name}/#{self.id}"
end
|
#mime_type ⇒ Object
76
77
78
|
# File 'lib/mongoid_ext/file.rb', line 76
def mime_type
self.content_type || get.content_type
end
|
#put(filename, io, options = {}) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/mongoid_ext/file.rb', line 13
def put(filename, io, options = {})
mark_parent!
options[:_id] = grid_filename
options[:metadata] ||= {}
options[:metadata][:collection] = _root_document.collection.name
self["name"] = filename
if filename =~ /\.([\w]{2,4})$/
self["extension"] = $1
end
if io.kind_of?(String)
io = StringIO.new(io)
end
if defined?(Magic) && Magic.respond_to?(:guess_string_mime_type)
data = io.read(256) self["content_type"] = options[:content_type] = Magic.guess_string_mime_type(data.to_s)
if self.fetch("extension", nil).nil?
self["extension"] = options[:content_type].to_s.split("/").last.split("-").last
end
if io.respond_to?(:rewind)
io.rewind
else
io.seek(0)
end
end
options[:filename] = grid_filename
gridfs.delete(grid_filename)
gridfs.put(io, options)
if file = self.get
self['md5'] = file.md5
end
end
|
#read(size = nil) ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/mongoid_ext/file.rb', line 84
def read(size = nil)
if size != nil
puts "#{__FILE__}:#{__LINE__} Passing size to read() is deprecated and will be removed soon. Use .each {} to read in blocks."
end
self.get.data
end
|
#reset ⇒ Object
68
69
70
|
# File 'lib/mongoid_ext/file.rb', line 68
def reset
@io = nil
end
|
#size ⇒ Object
80
81
82
|
# File 'lib/mongoid_ext/file.rb', line 80
def size
get.file_length rescue nil
end
|