tinkit 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/LICENSE +176 -0
  2. data/README +11 -0
  3. data/Rakefile +75 -0
  4. data/lib/glue_envs/couchrest/couchrest_attachment_handler.rb +260 -0
  5. data/lib/glue_envs/couchrest/couchrest_files_mgr.rb +198 -0
  6. data/lib/glue_envs/couchrest_glue_env.rb +536 -0
  7. data/lib/glue_envs/files_mgr_base.rb +51 -0
  8. data/lib/glue_envs/filesystem/filesystem_files_mgr.rb +187 -0
  9. data/lib/glue_envs/filesystem_glue_env.rb +395 -0
  10. data/lib/glue_envs/mysql/mysql_files_mgr.rb +175 -0
  11. data/lib/glue_envs/mysql_glue_env.rb +428 -0
  12. data/lib/glue_envs/sdb_s3/sdb_s3_files_mgr.rb +314 -0
  13. data/lib/glue_envs/sdb_s3_glue_env.rb +248 -0
  14. data/lib/helpers/camel.rb +21 -0
  15. data/lib/helpers/filesystem_helpers.rb +27 -0
  16. data/lib/helpers/hash_helpers.rb +74 -0
  17. data/lib/helpers/log_helper.rb +34 -0
  18. data/lib/helpers/mime_types_new.rb +126 -0
  19. data/lib/helpers/old_more_open_struct.rb +28 -0
  20. data/lib/helpers/require_helper.rb +45 -0
  21. data/lib/helpers/tk_escape.rb +17 -0
  22. data/lib/midas/bufs_data_structure.rb +84 -0
  23. data/lib/midas/node_element_operations.rb +264 -0
  24. data/lib/tinkit.rb +38 -0
  25. data/lib/tinkit_base_node.rb +733 -0
  26. data/lib/tinkit_node_factory.rb +47 -0
  27. data/spec/couchrest_files_mgr_spec.rb +551 -0
  28. data/spec/couchrest_glue_spec.rb +246 -0
  29. data/spec/filesystem_files_mgr_spec.rb +236 -0
  30. data/spec/filesystem_glue_spec.rb +243 -0
  31. data/spec/filesystem_helpers_spec.rb +42 -0
  32. data/spec/helpers/bufs_node_builder.rb +17 -0
  33. data/spec/helpers/bufs_sample_dataset.rb +160 -0
  34. data/spec/helpers/bufs_test_environments.rb +81 -0
  35. data/spec/helpers/tmp_view_cleaner.rb +15 -0
  36. data/spec/lib_helpers/tk_escape_spec.rb +45 -0
  37. data/spec/mysql_files_mgr_spec.rb +250 -0
  38. data/spec/mysql_glue_spec.rb +214 -0
  39. data/spec/node_element_operations_spec.rb +392 -0
  40. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec1.rb +82 -0
  41. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec2.rb +68 -0
  42. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec3.rb +80 -0
  43. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec4.rb +110 -0
  44. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec5.rb +84 -0
  45. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec6.rb +83 -0
  46. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec7.rb +101 -0
  47. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec8.rb +92 -0
  48. data/spec/sdb_s3_files_mgr_spec/sdb_s3_files_mgr_spec_all.rb +266 -0
  49. data/spec/sdb_s3_glue_spec.rb +230 -0
  50. data/spec/tinkit_node_factory_spec.rb +1108 -0
  51. metadata +114 -0
data/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
data/README ADDED
@@ -0,0 +1,11 @@
1
+ Bufs
2
+
3
+ The current core classes give the ability to synchronize a file system to the backend repository (a couch database) in such a way that tagging (called categories) is allowed. These categories aren't your run of the mill tags, they can also be used to categorize categories as well as content (i.e., you can tag the tags). This allows you to organize data organically.
4
+
5
+ Future developments will be to provide a web interface, and also a file system "view" where the directory structure mirrors the category organization.
6
+
7
+ Topics to cover in more detail
8
+ Node Synchonization
9
+ - Can synchronize but must be unique Class-Namespace combination
10
+ Abstract Node Format
11
+ - namespace
@@ -0,0 +1,75 @@
1
+ #The specs in this rake file deal only with the core
2
+ #bufs libraries
3
+
4
+ require 'rake'
5
+ require 'spec/rake/spectask'
6
+
7
+
8
+ #Allows clearing of the task environment
9
+ class Rake::Task
10
+ def abandon
11
+ @actions.clear
12
+ end
13
+ end
14
+ #task :default => ['specs_with_rcov']
15
+
16
+ #Tests that fail in rake but work standalone
17
+ spec_set_0 = ['spec/tk_escape_spec.rb']
18
+
19
+ #fixture tests
20
+ spec_set_1 = ['spec/couchdb_running_spec.rb',
21
+ 'spec/bufs_sample_dataset_spec.rb']
22
+
23
+ #data structure tests (currently not working)
24
+ spec_set_2 = ['spec/node_element_operations_spec.rb']
25
+
26
+ #model tests for multi-user
27
+ spec_set_3 = ['spec/tinkit_node_factory_spec.rb']
28
+
29
+ #These tests would share common variable namespaces
30
+ #and will clobber each other if ran in a common environment
31
+ spec_set_3aa = ['spec/couchrest_attachment_handler_spec.rb']
32
+ #'spec/bufs_base_node_spec.rb',
33
+ spec_set_3a = [ 'spec/bufs_couchrest_spec.rb']
34
+
35
+ spec_set_3b = ['spec/bufs_filesystem_spec.rb']
36
+
37
+ #Set up rake specs by spec sets"
38
+ spec_sets = { "spec_set_0" => spec_set_0,
39
+ "spec_set_1" => spec_set_1,
40
+ "spec_set_2" => spec_set_2,
41
+ "spec_set_3" => spec_set_3,
42
+ "spec_set_3a"=> spec_set_3a,
43
+ "spec_set_3aa"=>spec_set_3aa,
44
+ "spec_set_3b"=> spec_set_3b
45
+ }
46
+
47
+
48
+ #creates spec task spec_set_##
49
+ spec_sets.each do |name, set|
50
+ desc "Run #{name}"
51
+ Spec::Rake::SpecTask.new(name) do |t|
52
+ puts "Creating set: #{name}"
53
+ t.spec_files = spec_sets[name]
54
+ t.rcov = false
55
+ end
56
+ end
57
+
58
+
59
+ desc "Runs all specs in their environments and continues if error is encountered"
60
+ task :spec_sets do
61
+ spec_sets.each do |name, set|
62
+ puts "Running Test on #{name}"
63
+ begin
64
+ Rake::Task[name].invoke
65
+ rescue => e
66
+ puts "Rake Task #{name} Failed"
67
+ puts "Error message: #{e.inspect}"
68
+ #puts "Trace:\n #{e.backtrace}"
69
+ puts "Moving on to next set"
70
+ next
71
+ end
72
+ puts "Clearing Task"
73
+ Rake::Task[name].abandon
74
+ end
75
+ end
@@ -0,0 +1,260 @@
1
+ require 'cgi'
2
+ require 'couchrest'
3
+
4
+ #Tinkit directory organization defined in lib/helpers/require_helper.rb
5
+ require Tinkit.helpers 'tk_escape'
6
+ require Tinkit.helpers 'mime_types_new'
7
+
8
+ #Performs manipulations ont file attachment structures and metadata
9
+ module CouchrestAttachmentHelpers
10
+
11
+ #Attachment data format: attachment_name => attachment info
12
+ #attachment info format: { 'data' => attachment data, 'md' => attachment metadata }
13
+ #attachment data is sorted into the data and metadata CouchDB attachments can handle natively
14
+ #and the additional metadata that CouchDB attachments do not handle (boo, hiss)
15
+ #
16
+ # Usage Example:
17
+ # CouchrestAttachmentHelpers.sort_attachment_data(attachments)
18
+ # #=> { 'data_by_name' => { attachment_1 => raw_attachment_data1,
19
+ # attachment_2 => raw_attachment_data2 }.
20
+ # 'att_md_by_name' => { attachment_1 => CouchDB metadata fields1,
21
+ # attachment_2 => CouchDB metadata fields2}
22
+ # 'cust_md_by_name' => { attachment_1 => Custom metadata fields1,
23
+ # attachment_2 => Custom metadata fields2}
24
+ # }
25
+ def self.sort_attachment_data(attachments)
26
+ all_couch_attach_params = {}
27
+ all_custom_attach_params = {}
28
+ all_attach_data = {}
29
+ attachments.each do |att_name, att_info|
30
+ #att_info: 'data' => att data, 'md' => att metadata
31
+ esc_att_name = TkEscape.escape(att_name)
32
+ att_params = {}
33
+ obj_params = {}
34
+ attach_data = nil
35
+ att_info.each do |info, info_value|
36
+ if info == 'data'
37
+ attach_data = info_value
38
+ elsif info == 'md'
39
+ #md holds all file metadata (both couch and custom)
40
+ split_metadata = self.split_attachment_metadata(info_value)
41
+ att_params = split_metadata['att_md']
42
+ obj_params = split_metadata['cust_md']
43
+ end
44
+ end
45
+ all_couch_attach_params[esc_att_name] = att_params
46
+ all_custom_attach_params[esc_att_name] = obj_params
47
+ all_attach_data[esc_att_name] = attach_data
48
+ end
49
+ sorted = {'data_by_name' => all_attach_data,
50
+ 'att_md_by_name' => all_couch_attach_params,
51
+ 'cust_md_by_name' => all_custom_attach_params}
52
+ return sorted
53
+ end
54
+
55
+ #Escapes attachment names in a CouchDB compatible way
56
+ def self.escape_names_in_attachments(unesc_attachments)
57
+ escaped_attachments = {}
58
+ unesc_attachments.each do |unesc_key, val|
59
+ esc_key = TkEscape.escape(unesc_key)
60
+ escaped_attachments[esc_key] = val
61
+ end
62
+ return escaped_attachments
63
+ end
64
+
65
+ #Unescapes attachment names in a CouchDb compatible way
66
+ def self.unescape_names_in_attachments(esc_attachments)
67
+ unescaped_attachments = {}
68
+ #esc_attachments = esc_attachments || []
69
+ esc_attachments.each do |esc_key, val|
70
+ unesc_key = CGI.unescape(esc_key)
71
+ unescaped_attachments[unesc_key] = val
72
+ end
73
+ return unescaped_attachments
74
+ end
75
+
76
+ private
77
+
78
+ #Takes the abstracted attachment data and splits it into
79
+ #the data used by this class and the underlying CouchDB format
80
+ def self.split_attachment_metadata(combined_metadata)
81
+ split_metadata = {'cust_md' => {}, 'att_md' => {}}
82
+ combined_metadata.each do |param, param_value|
83
+ if CouchrestAttachment::CouchDBAttachParams.include? param
84
+ split_metadata['att_md'][param] = param_value
85
+ else
86
+ split_metadata['cust_md'][param] = param_value
87
+ end
88
+ end
89
+ return split_metadata
90
+ end
91
+
92
+ end
93
+
94
+
95
+ #Converts from Tinkit attachment format to closer to the metal
96
+ #couchrest/CouchDB attachment format. The reason this is needed is because
97
+ #CouchDB cannot support custom metadata for attachments. So custom metadata is held
98
+ #in the CouchrestAttachment document. This document will also hold the
99
+ #attachments and its built in metadata (such as content-type and modified times
100
+ # Attachment structure:
101
+ # attachments =>{ attachment_1 => { 'data1' => raw attachment data1,
102
+ # 'md1' => combined attachment metadata1 },
103
+ # attachment_2 => { 'data2' => raw attachment data2,
104
+ # 'md2' => combined attachment metadata2 }
105
+ # }
106
+ #
107
+
108
+ class CouchrestAttachment < CouchRest::ExtendedDocument
109
+
110
+ #CouchDB attachment metadata parameters supported by CouchrestAttachment
111
+ CouchDBAttachParams = ['content_type', 'stub']
112
+
113
+ #changing this will result in existing persisted data being lost
114
+ #(unless the persisted data is updated as well)
115
+ AttachmentID = "_attachments"
116
+
117
+ #create the attachment document id to be used
118
+ def self.uniq_att_doc_id(doc_id)
119
+ uniq_id = doc_id + AttachmentID if doc_id#bufs_node.class.attachment_base_id
120
+ end
121
+
122
+ def self.add_attachment_package(doc_id, attach_class, attachments)
123
+ raise "No class definition provided for attachments" unless attach_class
124
+ raise "No id found for the document" unless doc_id #bufs_node._model_metadata[:_id]
125
+ raise "No attachments provided for attaching" unless attachments
126
+ att_doc_id = self.uniq_att_doc_id(doc_id)
127
+ att_doc = self.get(att_doc_id)
128
+ rtn = if att_doc
129
+ self.update_attachment_package(att_doc, attachments)
130
+ else
131
+ self.create_attachment_package(att_doc_id, attach_class, attachments)
132
+ end
133
+ return rtn
134
+ end
135
+
136
+ #Create an attachment for a particular BUFS document
137
+ def self.create_attachment_package(att_doc_id, attach_class, attachments)
138
+ raise "No class definition provided for attachments" unless attach_class
139
+ raise "No id found for the document" unless att_doc_id
140
+ raise "No attachments provided for attaching" unless attachments
141
+
142
+ sorted_attachments = CouchrestAttachmentHelpers.sort_attachment_data(attachments)
143
+ custom_metadata_doc_params = {'_id' => att_doc_id, 'md_attachments' => sorted_attachments['cust_md_by_name']}
144
+ att_doc = attach_class.new(custom_metadata_doc_params)
145
+ att_doc.save
146
+
147
+ sorted_attachments['att_md_by_name'].each do |att_name, params|
148
+ esc_att_name = TkEscape.escape(att_name)
149
+ att_doc.put_attachment(esc_att_name, sorted_attachments['data_by_name'][esc_att_name],params)
150
+ end
151
+
152
+ #returns the updated document from the database
153
+ return self.get(att_doc_id)
154
+ end
155
+
156
+ #not used? (2010-11-04)
157
+ #def update_attachment_package(att_doc_id, attach_class, new_attachments)
158
+ # #bufs_doc.my_GlueEnv.attachClass.update_attachment_package(self, new_attachments)
159
+ # self.update_attachment_package(att_doc_id, attach_class, new_attachments)
160
+ #end
161
+
162
+ #Update the attachment data for a particular BUFS document
163
+ # Important Note: Currently existing data is only updated if new data has been modified more recently than the existing data.
164
+ def self.update_attachment_package(att_doc, new_attachments)
165
+ existing_attachments = att_doc.get_attachments
166
+ most_recent_attachment = {}
167
+ if existing_attachments
168
+ new_attachments.each do |new_att_name, new_data|
169
+ esc_new_att_name = TkEscape.escape(new_att_name)
170
+ working_doc = att_doc.class.get(att_doc['_id'])
171
+ if existing_attachments.keys.include? esc_new_att_name
172
+ #filename already exists as an attachment
173
+ fresh_attachment =self.find_most_recent_attachment(existing_attachments[esc_new_att_name], new_attachments[new_att_name]['md'])
174
+ most_recent_attachment[esc_new_att_name] = fresh_attachment
175
+
176
+ #re-add the if statement to prevent old from overwriting newer files
177
+ ###if most_recent_attachment[esc_new_att_name] != existing_attachments[esc_new_att_name]
178
+ #update that file and metadata
179
+ sorted_attachments = CouchrestAttachmentHelpers.sort_attachment_data(esc_new_att_name => new_data)
180
+ #update doc
181
+ working_doc['md_attachments'] = working_doc['md_attachments'].merge(sorted_attachments['cust_md_by_name'])
182
+ #update attachments
183
+ working_doc.save
184
+ #Add Couch attachment data
185
+ att_data = sorted_attachments['data_by_name'][esc_new_att_name]
186
+ att_md = sorted_attachments['att_md_by_name'][esc_new_att_name]
187
+ working_doc.put_attachment(esc_new_att_name, att_data,att_md)
188
+ ###else
189
+ #do anything here?
190
+ #puts "Warning, didn't update the attachment because current attachment is older than present one"
191
+ ###end
192
+ else #filename does not exist in attachment
193
+ #puts "Attachment Name not found in Attachment Document, adding #{esc_new_att_name}"
194
+ sorted_attachments = CouchrestAttachmentHelpers.sort_attachment_data(esc_new_att_name => new_data)
195
+ #update doc
196
+ working_doc['md_attachments'] = working_doc['md_attachments'].merge(sorted_attachments['cust_md_by_name'])
197
+ #update attachments
198
+ working_doc.save
199
+ #Add Couch attachment data
200
+ att_data = sorted_attachments['data_by_name'][esc_new_att_name]
201
+ att_md = sorted_attachments['att_md_by_name'][esc_new_att_name]
202
+ working_doc.put_attachment(esc_new_att_name, att_data,att_md)
203
+ #working_doc does not have attachment
204
+
205
+ end
206
+
207
+ end
208
+ end
209
+ return att_doc.class.get(att_doc['_id'])
210
+ end
211
+
212
+ #retrieves document attachments for a particular document
213
+ def self.get_attachments(att_doc)
214
+ return nil unless att_doc
215
+ custom_md = att_doc['md_attachments']
216
+ esc_couch_md = att_doc['_attachments']
217
+ couch_md = CouchrestAttachmentHelpers.unescape_names_in_attachments(esc_couch_md)
218
+ if custom_md.keys.sort != couch_md.keys.sort
219
+ raise "data integrity error, attachment metadata inconsistency\n"\
220
+ "in memory: #{custom_md.inspect} \n persisted: #{couch_md.inspect}"
221
+ end
222
+ (attachment_data = custom_md.dup).merge(couch_md) {|k,v_custom, v_couch| v_custom.merge(v_couch)}
223
+ end
224
+
225
+ #retrieves document attachments for this document
226
+ def get_attachments
227
+ self.class.get_attachments(self)
228
+ end
229
+
230
+ def remove_attachment(attachment_names)
231
+ attachment_names = [attachment_names].flatten
232
+ attachment_names.each do |att_name|
233
+ att_name = TkEscape.escape(att_name)
234
+ self['md_attachments'].delete(att_name)
235
+ self['_attachments'].delete(att_name)
236
+ end
237
+ resp = self.save
238
+ atts = self.class.get_attachments(self)
239
+ raise "Remove Attachment Operation Failed with response: #{resp.inspect}" unless resp == true
240
+ self
241
+ end
242
+
243
+ private
244
+
245
+ def self.find_most_recent_attachment(attachment_data1, attachment_data2)
246
+ #"Finding most recent attachment"
247
+ most_recent_attachment_data = nil
248
+ if attachment_data1 && attachment_data2
249
+ if attachment_data1['file_modified'] >= attachment_data2['file_modified']
250
+ most_recent_attachment_data = attachment_data1
251
+ else
252
+ most_recent_attachment_data = attachment_data2
253
+ end
254
+ else
255
+ most_recent_attachment_data = attachment_data1 || attachment_data2
256
+ end
257
+ most_recent_attachment_data
258
+ end#def
259
+
260
+ end#class