mongo_odm_grid 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.txt ADDED
@@ -0,0 +1,45 @@
1
+ Summary
2
+
3
+ MongoODM::Document::Grid is a Grid plugin for MongoODM
4
+
5
+ Support is built in for rack_grid and rack_grid_thumb to generate URLS and thumbnails:
6
+ http://github.com/dusty/rack_grid
7
+ http://github.com/dusty/rack_grid_thumb
8
+
9
+ Installation
10
+
11
+ # gem install mongo_odm_grid
12
+
13
+ Usage
14
+
15
+ require 'mongo_odm_grid'
16
+
17
+ class Monkey
18
+ include MongoODM::Document::Grid
19
+
20
+ attachment :image, :prefix => :grid
21
+ end
22
+
23
+ m = Monkey.new(:name => 'name')
24
+ m.save
25
+
26
+ # To add an attachment
27
+ m.image = File.open('/tmp/me.jpg')
28
+ m.save
29
+
30
+ # To remove an attachment
31
+ m.image = nil
32
+ m.save
33
+
34
+ # To get the attachment
35
+ m.image.read
36
+
37
+ # To get the URL for rack_grid
38
+ m.image_url # /grid/4e049e7c69c3b27d53000005/me.jpg
39
+
40
+ # To get the thumbail URL for rack_grid_thumb
41
+ m.image_thumb('50x50') # /grid/4e049e7c69c3b27d53000005/me_50x50.jpg
42
+
43
+ Inspired By
44
+ - http://github.com/jnunemaker/joint
45
+
@@ -0,0 +1,243 @@
1
+ require 'mime/types'
2
+
3
+ module MongoODM
4
+ module Document
5
+ module Grid
6
+
7
+ def self.included(base)
8
+ base.send(:include, InstanceMethods)
9
+ base.send(:extend, ClassMethods)
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ ##
15
+ # Declare an attachment for the object
16
+ #
17
+ # eg: attachment :image
18
+ def attachment(name,options={})
19
+ prefix = options[:prefix] ||= :grid
20
+
21
+ ##
22
+ # Callbacks to handle the attachment saving and deleting
23
+ after_save :create_attachments
24
+ after_save :delete_attachments
25
+ after_destroy :queue_delete_attachments
26
+ after_destroy :delete_attachments
27
+
28
+ ##
29
+ # Fields for the attachment.
30
+ #
31
+ # Only the _id is really needed, the others are helpful cached
32
+ # so you don't need to hit GridFS
33
+ field "#{name}_id".to_sym, BSON::ObjectId
34
+ field "#{name}_name".to_sym, String
35
+ field "#{name}_size".to_sym, Integer
36
+ field "#{name}_type".to_sym, String
37
+
38
+ ##
39
+ # Add this name to the attachment_types
40
+ attachment_types.push(name).uniq!
41
+
42
+ ##
43
+ # Return the Grid object.
44
+ # eg: image.filename, image.read
45
+ define_method(name) do
46
+ grid.get(read_attribute("#{name}_id")) if read_attribute("#{name}_id")
47
+ end
48
+
49
+ ##
50
+ # Create a method to set the attachment
51
+ # eg: object.image = File.open('/tmp/somefile.jpg')
52
+ define_method("#{name}=") do |file|
53
+ # delete the old file if it exists
54
+ unless read_attribute("#{name}_id").blank?
55
+ send(:delete_attachment, name, read_attribute("#{name}_id"))
56
+ end
57
+ case
58
+ when file.is_a?(Hash) && file[:tempfile]
59
+ send(:create_attachment, name, file)
60
+ when file.respond_to?(:read)
61
+ send(:create_attachment, name, file)
62
+ end
63
+ end
64
+
65
+ ##
66
+ # Create a method to set the attachment for binary string.
67
+ # eg: object.set_image(binary_string, "generated_filename.png")
68
+ define_method("set_#{name}") do |binary, filename|
69
+ if !binary.nil?
70
+ send(:create_attachment_raw, name, binary, filename)
71
+ else
72
+ send(:delete_attachment, name, read_attribute("#{name}_id"))
73
+ end
74
+ end
75
+
76
+ ##
77
+ # Unset the attachment, queue for removal
78
+ define_method("unset_#{name}") do
79
+ send(:delete_attachment, name, read_attribute("#{name}_id"))
80
+ end
81
+
82
+ ##
83
+ # Return the relative URL to the attachment for use with Rack::Grid
84
+ # eg: /grid/4ba69fde8c8f369a6e000003/somefile.png
85
+ define_method("#{name}_url") do
86
+ _id = read_attribute("#{name}_id")
87
+ _name = read_attribute("#{name}_name")
88
+ ["/#{prefix}", _id, _name].join('/') if _id && _name
89
+ end
90
+
91
+ ##
92
+ # Return the relative URL to the thumbnail for use with Rack::GridThumb
93
+ # eg: /grid/4ba69fde8c8f369a6e000003/somefile_50x.png
94
+ define_method("#{name}_thumb") do |thumb|
95
+ _id = read_attribute("#{name}_id")
96
+ _name = read_attribute("#{name}_name")
97
+ _ext = File.extname(_name)
98
+ _base = File.basename(_name,_ext)
99
+ _name = "#{_base}_#{thumb}#{_ext}"
100
+ ["/#{prefix}", _id, _name].join('/') if _id && _name
101
+ end
102
+ end
103
+
104
+ ##
105
+ # Accessor to Grid
106
+ def grid
107
+ @grid ||= Mongo::Grid.new(MongoODM.database)
108
+ end
109
+
110
+ ##
111
+ # All the attachments types for this class
112
+ def attachment_types
113
+ @attachment_types ||= []
114
+ end
115
+
116
+ end
117
+
118
+ module InstanceMethods
119
+
120
+ ##
121
+ # Accessor to Grid
122
+ def grid
123
+ self.class.grid
124
+ end
125
+
126
+ private
127
+ ##
128
+ # Holds queue of attachments to create
129
+ def create_attachment_queue
130
+ @create_attachment_queue ||= {}
131
+ end
132
+
133
+ ##
134
+ # Holds queue of attachments to delete
135
+ def delete_attachment_queue
136
+ @delete_attachment_queue ||= {}
137
+ end
138
+
139
+ ##
140
+ # Attachments we need to add after save.
141
+ def create_attachment(name,file)
142
+ case
143
+ when file.is_a?(Hash)
144
+ filename = file[:filename]
145
+ size = File.size(file[:tempfile])
146
+ mime = file[:type]
147
+ unless mime
148
+ type = MIME::Types.type_for(filename).first
149
+ mime = type ? type.content_type : "application/octet-stream"
150
+ end
151
+ when file.respond_to?(:read)
152
+ filename = case
153
+ when file.respond_to?(:original_filename) && file.original_filename
154
+ file.original_filename
155
+ when file.respond_to?(:tempfile)
156
+ File.basename(file.tempfile.path)
157
+ else
158
+ File.basename(file.path)
159
+ end
160
+ size = File.size(file.respond_to?(:tempfile) ? file.tempfile : file)
161
+ type = MIME::Types.type_for(filename).first
162
+ mime = type ? type.content_type : "application/octet-stream"
163
+ else
164
+ return
165
+ end
166
+ write_attribute("#{name}_id", BSON::ObjectId.new)
167
+ write_attribute("#{name}_name", filename)
168
+ write_attribute("#{name}_size", size)
169
+ write_attribute("#{name}_type", mime)
170
+ create_attachment_queue[name] = file
171
+ end
172
+
173
+ ##
174
+ # Attachments we need to add after save.
175
+ # For binary String data.
176
+ def create_attachment_raw(name, binary, filename)
177
+ type = MIME::Types.type_for(filename).first
178
+ mime = type ? type.content_type : "application/octet-stream"
179
+ write_attribute("#{name}_id", BSON::ObjectId.new)
180
+ write_attribute("#{name}_name", filename)
181
+ write_attribute("#{name}_size", binary.size)
182
+ write_attribute("#{name}_type", mime)
183
+ create_attachment_queue[name] = binary
184
+ end
185
+
186
+ ##
187
+ # Save an attachment to Grid
188
+ def create_grid_attachment(name,file)
189
+ data = case
190
+ when file.is_a?(Hash)
191
+ file[:tempfile].read
192
+ else
193
+ file.respond_to?(:read) ? file.read : file
194
+ end
195
+ grid.put(
196
+ data,
197
+ :filename => read_attribute("#{name}_name"),
198
+ :content_type => read_attribute("#{name}_type"),
199
+ :_id => read_attribute("#{name}_id")
200
+ )
201
+ create_attachment_queue.delete(name)
202
+ end
203
+
204
+ ##
205
+ # Attachments we need to remove after save
206
+ def delete_attachment(name,id)
207
+ delete_attachment_queue[name] = id if id.is_a?(BSON::ObjectId)
208
+ write_attribute("#{name}_id", nil)
209
+ write_attribute("#{name}_name", nil)
210
+ write_attribute("#{name}_size", nil)
211
+ write_attribute("#{name}_type", nil)
212
+ end
213
+
214
+ ##
215
+ # Delete an attachment from Grid
216
+ def delete_grid_attachment(name,id)
217
+ grid.delete(id) if id.is_a?(BSON::ObjectId)
218
+ delete_attachment_queue.delete(name)
219
+ end
220
+
221
+ ##
222
+ # Create attachments marked for creation
223
+ def create_attachments
224
+ create_attachment_queue.each {|k,v| create_grid_attachment(k,v)}
225
+ end
226
+
227
+ ##
228
+ # Delete attachments marked for deletion
229
+ def delete_attachments
230
+ delete_attachment_queue.each {|k,v| delete_grid_attachment(k,v)}
231
+ end
232
+
233
+ ##
234
+ # Queues all attachments for deletion
235
+ def queue_delete_attachments
236
+ self.class.attachment_types.each do |name|
237
+ delete_attachment(name, read_attribute("#{name}_id"))
238
+ end
239
+ end
240
+ end
241
+ end
242
+ end
243
+ end
File without changes
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_odm_grid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dusty Doris
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-08 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mime-types
16
+ requirement: &2153373660 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2153373660
25
+ description: Plugin for MongoODM to attach files via Grid
26
+ email: github@dusty.name
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files:
30
+ - README.txt
31
+ files:
32
+ - README.txt
33
+ - lib/mongo_odm_grid.rb
34
+ - test/test_mongo_odm_grid.rb
35
+ homepage: http://github.com/dusty/mongo_odm_grid
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project: none
55
+ rubygems_version: 1.8.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Plugin for MongoODM to attach files via Grid
59
+ test_files: []