lib/rid/attachments.rb in rid-0.3.0 vs lib/rid/attachments.rb in rid-0.3.1
- old
+ new
@@ -5,44 +5,53 @@
".html" => "text/html",
".js" => "text/javascript",
".css" => "text/css",
}
- def reduce_attachments!
- return hash unless hash["_attachments"]
- attachments = {}
- hash["_attachments"].each do |key, value|
- data = value["data"]
- next unless data
- attachments.update key => decode_attachment(data)
- end
- hash.update "_attachments" => attachments
- end
+ # encode attachments and add meta data
+ #
def map_attachments!
- return unless hash["_attachments"]
- attachments = {}
- flatten_attachements(hash["_attachments"]).each do |key, value|
- attachments.update key => {
+ return if attachments.empty?
+
+ doc = {}
+ attachments.flatten.each do |key, value|
+ doc[key] = {
"data" => encode_attachment(value),
"content_type" => mime_type_for(key)
}
end
- self.hash.update "_attachments" => attachments
+
+ self.attachments = doc
end
- def flatten_attachements(doc, base = nil)
- result = {}
- doc.each do |key, value|
- new_base = base ? [base, key].join('/') : key
- if value.is_a?(Hash)
- result.update flatten_attachements(value, new_base)
- else
- result.update new_base => value
- end
+ # decode attachments and flatten meta data hash
+ #
+ def reduce_attachments!
+ return if attachments.empty?
+
+ doc = {}
+ attachments.each do |key, value|
+ data = value["data"]
+ next unless data
+ doc[key] = decode_attachment(data)
end
- result
+
+ self.attachments = doc
end
+
+ private
+
+ # accessor for attachments hash
+ #
+ def attachments
+ hash["_attachments"] || {}
+ end
+
+ def attachments=(value)
+ self.hash["_attachments"] = value
+ end
+
def decode_attachment(data)
data.unpack("m").first
end