module CouchrestAttachmentHelpers
Performs manipulations ont file attachment structures and metadata
Public Class Methods
escape_names_in_attachments(unesc_attachments)
click to toggle source
Escapes attachment names in a CouchDB compatible way
# File lib/glue_envs/couchrest/couchrest_attachment_handler.rb, line 65 def self.escape_names_in_attachments(unesc_attachments) escaped_attachments = {} unesc_attachments.each do |unesc_key, val| esc_key = TkEscape.escape(unesc_key) escaped_attachments[esc_key] = val end return escaped_attachments end
sort_attachment_data(attachments)
click to toggle source
Attachment data format: attachment_name => attachment info attachment info format: { 'data' => attachment data, 'md' => attachment metadata } attachment data is sorted into the data and metadata CouchDB attachments can handle natively and the additional metadata that CouchDB attachments do not handle (boo, hiss)
Usage Example: CouchrestAttachmentHelpers.sort_attachment_data(attachments) #=> { 'data_by_name' => { attachment_1 => raw_attachment_data1, attachment_2 => raw_attachment_data2 }. 'att_md_by_name' => { attachment_1 => CouchDB metadata fields1, attachment_2 => CouchDB metadata fields2} 'cust_md_by_name' => { attachment_1 => Custom metadata fields1, attachment_2 => Custom metadata fields2} }
# File lib/glue_envs/couchrest/couchrest_attachment_handler.rb, line 27 def self.sort_attachment_data(attachments) all_couch_attach_params = {} all_custom_attach_params = {} all_attach_data = {} attachments.each do |att_name, att_info| #att_info: 'data' => att data, 'md' => att metadata esc_att_name = TkEscape.escape(att_name) att_params = {} obj_params = {} attach_data = nil att_info.each do |info, info_value| if info == 'data' attach_data = info_value elsif info == 'md' #md holds all file metadata (both couch and custom) split_metadata = self.split_attachment_metadata(info_value) att_params = split_metadata['att_md'] obj_params = split_metadata['cust_md'] end end #Not completely sure converting content type to a symbol was the problem, # but it works now #Investigate further time permitting. #couchrest change forced this workaround att_params[:content_type] = att_params['content_type'] att_params.delete('content_type') all_couch_attach_params[esc_att_name] = att_params all_custom_attach_params[esc_att_name] = obj_params all_attach_data[esc_att_name] = attach_data end sorted = {'data_by_name' => all_attach_data, 'att_md_by_name' => all_couch_attach_params, 'cust_md_by_name' => all_custom_attach_params} return sorted end
unescape_names_in_attachments(esc_attachments)
click to toggle source
Unescapes attachment names in a CouchDb compatible way
# File lib/glue_envs/couchrest/couchrest_attachment_handler.rb, line 75 def self.unescape_names_in_attachments(esc_attachments) unescaped_attachments = {} #esc_attachments = esc_attachments || [] esc_attachments.each do |esc_key, val| unesc_key = CGI.unescape(esc_key) unescaped_attachments[unesc_key] = val end return unescaped_attachments end
Private Class Methods
split_attachment_metadata(combined_metadata)
click to toggle source
Takes the abstracted attachment data and splits it into the data used by this class and the underlying CouchDB format
# File lib/glue_envs/couchrest/couchrest_attachment_handler.rb, line 89 def self.split_attachment_metadata(combined_metadata) split_metadata = {'cust_md' => {}, 'att_md' => {}} combined_metadata.each do |param, param_value| if CouchrestAttachment::CouchDBAttachParams.include? param split_metadata['att_md'][param] = param_value else split_metadata['cust_md'][param] = param_value end end return split_metadata end