grip 0.3.0

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/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ LICENSE
2
+
3
+ The MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ Grip
2
+ ==============
3
+
4
+ GridFS Attachment gem for MongoMapper
5
+
6
+ **Sample Class**
7
+
8
+ class Foo
9
+ include MongoMapper::Document
10
+ include Grip
11
+
12
+ has_grid_attachment :image
13
+ has_grid_attachment :pdf
14
+ end
15
+
16
+ @image = File.open("#{File.dirname(__FILE__)}/test/cthulhu.png",'r')
17
+ @pdf = File.open("#{File.dirname(__FILE__)}/sample.pdf",'r')
18
+ @foo = Foo.create(:img=>@image,:pdf=>@pdf)
data/lib/grip.rb ADDED
@@ -0,0 +1,76 @@
1
+ include Mongo
2
+ include GridFS
3
+ module Grip
4
+
5
+ module InstanceMethods
6
+
7
+ # Save Path: <classname>/<id>/<method_name>/<file_basename>
8
+ def file_save_path name,file_name
9
+ [self.class.to_s.underscore,self.id,name,file_name].join("/")
10
+ end
11
+
12
+ def save_attachments
13
+ self.class.attachment_definitions.each do |attachment|
14
+ name, file = attachment
15
+ if file.is_a?(File)
16
+
17
+ file_path = file.original_filename rescue file.path.split("/").last
18
+ path = file_save_path(name,file_path)
19
+
20
+ GridStore.open(self.class.database, path, 'w') do |f|
21
+ content_type = file.content_type rescue MIME::Types.type_for(file.path).first.content_type
22
+ f.content_type = content_type
23
+ f.puts file.read
24
+ end
25
+
26
+ self.send("#{name}_path=",path)
27
+ save_to_collection
28
+ end
29
+ end
30
+ end
31
+
32
+ def destroy_attached_files
33
+ self.class.attachment_definitions.each do |name, attachment|
34
+ GridStore.unlink(self.class.database, send("#{name}_path"))
35
+ end
36
+ end
37
+
38
+ def file_from_grid name
39
+ GridStore.open(self.class.database, send("#{name}_path"), 'r') {|f| f }
40
+ end
41
+
42
+ end
43
+ module ClassMethods
44
+
45
+ def has_grid_attachment name
46
+ include InstanceMethods
47
+
48
+ # thanks to thoughtbot's paperclip!
49
+ write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
50
+ attachment_definitions[name] = {}
51
+
52
+ define_method name do
53
+ file_from_grid name
54
+ end
55
+
56
+ define_method "#{name}=" do |file|
57
+ self.class.attachment_definitions[name] = file
58
+ end
59
+
60
+ key "#{name}_path".to_sym, String
61
+ end
62
+
63
+ def attachment_definitions
64
+ read_inheritable_attribute(:attachment_definitions)
65
+ end
66
+
67
+ end
68
+
69
+ def self.included(base)
70
+ base.extend Grip::ClassMethods
71
+ base.class_eval do |klass|
72
+ after_save :save_attachments
73
+ before_destroy :destroy_attached_files
74
+ end
75
+ end
76
+ end
data/test/cthulhu.png ADDED
Binary file
data/test/sample.pdf ADDED
Binary file
data/test/test_grip.rb ADDED
@@ -0,0 +1,58 @@
1
+ require "test_helper"
2
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/grip')
3
+
4
+
5
+ class Foo
6
+ include MongoMapper::Document
7
+ include Grip
8
+ key :name, String
9
+ has_grid_attachment :image
10
+ has_grid_attachment :pdf
11
+ end
12
+
13
+
14
+ class TestContent< Test::Unit::TestCase
15
+
16
+ context "A MongoMapper Document" do
17
+
18
+ context "with an attachment" do
19
+ setup do
20
+ @image = File.open("#{File.dirname(__FILE__)}/cthulhu.png",'r')
21
+ @pdf = File.open("#{File.dirname(__FILE__)}/sample.pdf",'r')
22
+
23
+ @document = Foo.create(:image=>@image,:pdf=>@pdf)
24
+ @from_collection = Foo.first
25
+ end
26
+
27
+ should "only define callbacks once" do
28
+ assert_equal(1, Foo.after_save.collect(&:method).count)
29
+ assert_equal(1, Foo.before_destroy.collect(&:method).count)
30
+ end
31
+
32
+ should "have correct mime type" do
33
+ assert_equal("image/png", @from_collection.image.content_type)
34
+ assert_equal("application/pdf", @from_collection.pdf.content_type)
35
+ end
36
+
37
+ should "respond to the dynamic keys" do
38
+ [:pdf_path,:image_path].each {|k| assert @document.respond_to? k }
39
+ end
40
+
41
+ should "have the correct paths" do
42
+ assert_equal("foo/#{@document.id}/image/cthulhu.png", @document.image_path)
43
+ assert_equal("foo/#{@document.id}/pdf/sample.pdf", @document.pdf_path)
44
+ end
45
+
46
+ should "cleanup attachments" do
47
+ img_path = "foo/#{@document._id}/image/cthulhu.png", @document.image_path
48
+ pdf_path = "foo/#{@document._id}/pdf/sample.pdf", @document.pdf_path
49
+
50
+ @document.destroy
51
+
52
+ assert !GridStore.exist?(MONGO_DB,img_path)
53
+ assert !GridStore.exist?(MONGO_DB,pdf_path)
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,25 @@
1
+ %w{rubygems mongomapper shoulda mongo/gridfs mime/types}.each {|f| require f}
2
+
3
+
4
+
5
+ MongoMapper.database = "test-attachments"
6
+
7
+ # For Grip
8
+ MONGO_DB = MongoMapper.database
9
+
10
+
11
+ class ActiveSupport::TestCase
12
+ # Drop all columns after each test case.
13
+ def teardown
14
+ MongoMapper.connection.drop_database "test-attachments"
15
+ MongoMapper.database = "test-attachments"
16
+ end
17
+
18
+ # Make sure that each test case has a teardown
19
+ # method to clear the db after each test.
20
+ def inherited(base)
21
+ base.define_method teardown do
22
+ super
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - twoism
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-23 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: GridFS attachments for MongoMapper
17
+ email: signalstatic@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .gitignore
27
+ - LICENSE
28
+ - lib/grip.rb
29
+ - test/cthulhu.png
30
+ - test/sample.pdf
31
+ - test/test_grip.rb
32
+ - test/test_helper.rb
33
+ - README.rdoc
34
+ has_rdoc: true
35
+ homepage: http://github.com/twoism/grip
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.5
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: GridFS attachments for MongoMapper
62
+ test_files:
63
+ - test/test_grip.rb
64
+ - test/test_helper.rb