Class: MongoidExt::FileList
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
allocate, #assign_id, field, #id, #initialize
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_list.rb', line 4
def list_name
@list_name
end
|
#parent_document ⇒ Object
Returns the value of attribute parent_document.
3
4
5
|
# File 'lib/mongoid_ext/file_list.rb', line 3
def parent_document
@parent_document
end
|
Class Method Details
.demongoize(v) ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/mongoid_ext/file_list.rb', line 87
def self.demongoize(v)
return if v.nil?
doc = self.class.new
v.each do |k,v|
doc[k] = MongoidExt::File.new(v)
end
doc
end
|
.mongoize(v) ⇒ Object
83
84
85
|
# File 'lib/mongoid_ext/file_list.rb', line 83
def self.mongoize(v)
Hash[v]
end
|
Instance Method Details
#delete(id) ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/mongoid_ext/file_list.rb', line 69
def delete(id)
mark_parent!
file = self.get(id)
super(id)
file.delete
end
|
#destroy_files ⇒ Object
77
78
79
80
81
|
# File 'lib/mongoid_ext/file_list.rb', line 77
def destroy_files
each_file do |id, file|
get(id).delete
end
end
|
#each_file(&block) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/mongoid_ext/file_list.rb', line 31
def each_file(&block)
(self.keys-["_id"]).each do |key|
file = self.get(key)
yield key, file
end
end
|
#files ⇒ Object
25
26
27
28
29
|
# File 'lib/mongoid_ext/file_list.rb', line 25
def files
ids = self.keys
ids.delete("_id")
ids.map {|v| get(v) }
end
|
#get(id) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/mongoid_ext/file_list.rb', line 38
def get(id)
mark_parent!
if id.kind_of?(MongoidExt::File)
self[id.id] = id
return id
end
id = id.to_s.gsub(".", "_")
file = self[id]
if file.nil?
file = self[id] = MongoidExt::File.new
elsif !file.kind_of?(MongoidExt::File) && file.kind_of?(::Hash)
file = self[id] = MongoidExt::File.new(file)
end
file._root_document = parent_document
file._list_name = self.list_name
file
end
|
#mark_parent! ⇒ Object
98
99
100
|
# File 'lib/mongoid_ext/file_list.rb', line 98
def mark_parent!
parent_document.send("#{list_name}_will_change!")
end
|
#put(id, io, metadata = {}) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/mongoid_ext/file_list.rb', line 6
def put(id, io, metadata = {})
mark_parent!
if !parent_document.new_record?
filename = id
if io.respond_to?(:original_filename)
filename = io.original_filename
elsif io.respond_to?(:path) && io.path
filename = ::File.basename(io.path)
elsif io.kind_of?(String)
io = StringIO.new(io)
end
get(id).put(filename, io, metadata)
else
(@_pending_files ||= {})[id] = [io, metadata]
end
end
|
#sync_files ⇒ Object
60
61
62
63
64
65
66
67
|
# File 'lib/mongoid_ext/file_list.rb', line 60
def sync_files
if @_pending_files
@_pending_files.each do |filename, data|
put(filename, data[0], data[1])
end
@_pending_files = nil
end
end
|