matic_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,44 @@
1
+ Summary
2
+
3
+ Mongomatic::Plugins::Grid is a Grid plugin for Mongomatic
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 matic_grid
12
+
13
+ Usage
14
+
15
+ require 'matic_grid'
16
+
17
+ class Monkey < Mongomatic::Base
18
+ include Mongomatic::Plugins::Grid
19
+
20
+ attachment :image, :prefix => 'grid'
21
+ end
22
+
23
+ m = Monkey.new(:name => 'name').insert
24
+
25
+ # To add an attachment
26
+ m.image = File.open('/tmp/me.jpg')
27
+ m.update
28
+
29
+ # To remove an attachment
30
+ m.image = nil
31
+ m.update
32
+
33
+ # To get the attachment
34
+ m.image.read
35
+
36
+ # To get the URL for rack_grid
37
+ m.image_url # /grid/4e049e7c69c3b27d53000005/me.jpg
38
+
39
+ # To get the thumbail URL for rack_grid_thumb
40
+ m.image_thumb('50x50') # /grid/4e049e7c69c3b27d53000005/me_50x50.jpg
41
+
42
+ Inspired By
43
+ - http://github.com/jnunemaker/grip
44
+
data/lib/matic_grid.rb ADDED
@@ -0,0 +1,225 @@
1
+ require 'mime/types'
2
+
3
+ class GridmaticObserver < Mongomatic::Observer
4
+ def after_insert_or_update(instance, opts)
5
+ instance.send(:create_attachments)
6
+ instance.send(:delete_attachments)
7
+ end
8
+ def before_remove(instance,opts)
9
+ instance.send(:queue_delete_attachments)
10
+ instance.send(:delete_attachments)
11
+ end
12
+ end
13
+
14
+ module Mongomatic
15
+ module Plugins
16
+ module Grid
17
+
18
+ def self.included(base)
19
+ base.send(:include, Mongomatic::Observable)
20
+ base.send(:observer, :GridmaticObserver)
21
+ base.send(:include, InstanceMethods)
22
+ base.send(:extend, ClassMethods)
23
+ end
24
+
25
+ module ClassMethods
26
+
27
+ ##
28
+ # Declare an attachment for the object
29
+ #
30
+ # eg: attachment :image
31
+ def attachment(name,prefix='grid')
32
+ ##
33
+ # Add this name to the attachment_types
34
+ attachment_types.push(name).uniq!
35
+
36
+ ##
37
+ # Return the Grid object.
38
+ # eg: image.filename, image.read
39
+ define_method(name) do
40
+ grid.get(self["#{name}_id"]) if self["#{name}_id"]
41
+ end
42
+
43
+ ##
44
+ # Create a method to set the attachment
45
+ # eg: object.image = File.open('/tmp/somefile.jpg')
46
+ define_method("#{name}=") do |file|
47
+ if file.respond_to?(:read)
48
+ send(:create_attachment, name, file)
49
+ else
50
+ send(:delete_attachment, name, self["#{name}_id"])
51
+ end
52
+ end
53
+
54
+ ##
55
+ # Create a method to set the attachment for binary string.
56
+ # eg: object.set_image(binary_string, "generated_filename.png")
57
+ define_method("set_#{name}") do |binary, filename|
58
+ if !binary.nil?
59
+ send(:create_attachment_raw, name, binary, filename)
60
+ else
61
+ send(:delete_attachment, name, self["#{name}_id"])
62
+ end
63
+ end
64
+
65
+ ##
66
+ # Unset the attachment, queue for removal
67
+ define_method("unset_#{name}") do
68
+ send(:delete_attachment, name, self["#{name}_id"])
69
+ end
70
+
71
+ ##
72
+ # Return the relative URL to the attachment for use with Rack::Grid
73
+ # eg: /grid/4ba69fde8c8f369a6e000003/somefile.png
74
+ define_method("#{name}_url") do
75
+ _id = self["#{name}_id"]
76
+ _name = self["#{name}_name"]
77
+ ["/#{prefix}", _id, _name].join('/') if _id && _name
78
+ end
79
+
80
+ ##
81
+ # Return the relative URL to the thumbnail for use with Rack::GridThumb
82
+ # eg: /grid/4ba69fde8c8f369a6e000003/somefile.png
83
+ define_method("#{name}_thumb") do |thumb|
84
+ _id = self["#{name}_id"]
85
+ _name = self["#{name}_name"]
86
+ base = File.basename(_name)
87
+ ext = File.extname(_name)
88
+ _name = "#{_name}_#{thumb}#{ext}"
89
+ ["/#{prefix}", _id, _name].join('/') if _id && _name
90
+ end
91
+
92
+ ##
93
+ # Helper methods for details about the file
94
+ # eg: image_name, image_size, image_type, image_id
95
+ %W{ #{name}_name #{name}_size #{name}_type #{name}_id }.each do |attr|
96
+ define_method(attr) do
97
+ self[attr]
98
+ end
99
+ end
100
+ end
101
+
102
+ ##
103
+ # Accessor to Grid
104
+ def grid
105
+ @grid ||= Mongo::Grid.new(Mongomatic.db)
106
+ end
107
+
108
+ ##
109
+ # All the attachments types for this class
110
+ def attachment_types
111
+ @attachment_types ||= []
112
+ end
113
+
114
+ end
115
+
116
+ module InstanceMethods
117
+
118
+ ##
119
+ # Accessor to Grid
120
+ def grid
121
+ self.class.grid
122
+ end
123
+
124
+ private
125
+ ##
126
+ # Holds queue of attachments to create
127
+ def create_attachment_queue
128
+ @create_attachment_queue ||= {}
129
+ end
130
+
131
+ ##
132
+ # Holds queue of attachments to delete
133
+ def delete_attachment_queue
134
+ @delete_attachment_queue ||= {}
135
+ end
136
+
137
+ ##
138
+ # Attachments we need to add after save.
139
+ def create_attachment(name,file)
140
+ if file.respond_to?(:read)
141
+ filename = if file.respond_to?(:original_filename) && file.original_filename
142
+ file.original_filename
143
+ elsif file.respond_to?(:tempfile)
144
+ File.basename(file.tempfile.path)
145
+ else
146
+ File.basename(file.path)
147
+ end
148
+ type = MIME::Types.type_for(filename).first
149
+ mime = type ? type.content_type : "application/octet-stream"
150
+ self["#{name}_id"] = BSON::ObjectId.new
151
+ self["#{name}_name"] = filename
152
+ self["#{name}_size"] = File.size(file.respond_to?(:tempfile) ? file.tempfile : file)
153
+ self["#{name}_type"] = mime
154
+ create_attachment_queue[name] = file
155
+ end
156
+ end
157
+
158
+ ##
159
+ # Attachments we need to add after save.
160
+ # For binary String data.
161
+ def create_attachment_raw(name, binary, filename)
162
+ type = MIME::Types.type_for(filename).first
163
+ mime = type ? type.content_type : "application/octet-stream"
164
+ self["#{name}_id"] = BSON::ObjectId.new
165
+ self["#{name}_name"] = filename
166
+ self["#{name}_size"] = binary.size
167
+ self["#{name}_type"] = mime
168
+ create_attachment_queue[name] = binary
169
+ end
170
+
171
+ ##
172
+ # Save an attachment to Grid
173
+ def create_grid_attachment(name,file)
174
+ data = file.respond_to?(:read) ? file.read : file
175
+ grid.put(
176
+ data,
177
+ :filename => self["#{name}_name"],
178
+ :content_type => self["#{name}_type"],
179
+ :_id => self["#{name}_id"]
180
+ )
181
+ create_attachment_queue.delete(name)
182
+ end
183
+
184
+ ##
185
+ # Attachments we need to remove after save
186
+ def delete_attachment(name,id)
187
+ delete_attachment_queue[name] = id if id.is_a?(BSON::ObjectId)
188
+ unless removed?
189
+ self["#{name}_id"] = nil
190
+ self["#{name}_name"] = nil
191
+ self["#{name}_size"] = nil
192
+ self["#{name}_type"] = nil
193
+ end
194
+ end
195
+
196
+ ##
197
+ # Delete an attachment from Grid
198
+ def delete_grid_attachment(name,id)
199
+ grid.delete(id) if id.is_a?(BSON::ObjectId)
200
+ delete_attachment_queue.delete(name)
201
+ end
202
+
203
+ ##
204
+ # Create attachments marked for creation
205
+ def create_attachments
206
+ create_attachment_queue.each {|k,v| create_grid_attachment(k,v)}
207
+ end
208
+
209
+ ##
210
+ # Delete attachments marked for deletion
211
+ def delete_attachments
212
+ delete_attachment_queue.each {|k,v| delete_grid_attachment(k,v)}
213
+ end
214
+
215
+ ##
216
+ # Queues all attachments for deletion
217
+ def queue_delete_attachments
218
+ self.class.attachment_types.each do |name|
219
+ delete_attachment(name, self["#{name}_id"])
220
+ end
221
+ end
222
+ end
223
+ end
224
+ end
225
+ end
File without changes
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: matic_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-06-24 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mime-types
16
+ requirement: &2156911780 !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: *2156911780
25
+ description: Plugin for Mongomatic 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/matic_grid.rb
34
+ - test/test_matic_grid.rb
35
+ homepage: http://github.com/dusty/matic_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 Mongomatic to attach files via Grid
59
+ test_files: []