Class: MongoidExt::File
Instance Attribute Summary collapse
Instance Method Summary
collapse
field, #id, #initialize
Constructor Details
This class inherits a constructor from EmbeddedHash
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/mongoid_ext/file.rb', line 72
def method_missing(name, *args, &block)
f = self.get rescue nil
if f && f.respond_to?(name)
f.send(name, *args, &block)
else
super(name, *args, &block)
end
end
|
Instance Attribute Details
#_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
#delete ⇒ Object
67
68
69
70
|
# File 'lib/mongoid_ext/file.rb', line 67
def delete
@io = nil
gridfs.delete(grid_filename)
end
|
#get ⇒ Object
43
44
45
|
# File 'lib/mongoid_ext/file.rb', line 43
def get
@io ||= gridfs.get(grid_filename)
end
|
#grid_filename ⇒ Object
51
52
53
|
# File 'lib/mongoid_ext/file.rb', line 51
def grid_filename
@grid_filename ||= "#{_root_document.collection.name}/#{self.id}"
end
|
#mime_type ⇒ Object
55
56
57
|
# File 'lib/mongoid_ext/file.rb', line 55
def mime_type
self.content_type || get.content_type
end
|
#put(filename, io, options = {}) ⇒ Object
11
12
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
|
# File 'lib/mongoid_ext/file.rb', line 11
def put(filename, io, options = {})
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)
self["extension"] ||= options[:content_type].to_s.split("/").last.split("-").last
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)
end
|
#read(size = nil) ⇒ Object
63
64
65
|
# File 'lib/mongoid_ext/file.rb', line 63
def read(size = nil)
self.get.read(size)
end
|
#reset ⇒ Object
47
48
49
|
# File 'lib/mongoid_ext/file.rb', line 47
def reset
@io = nil
end
|
#size ⇒ Object
59
60
61
|
# File 'lib/mongoid_ext/file.rb', line 59
def size
get.file_length
end
|