jonbell-grip 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ .DS_Store
2
+ **/.DS_Store
3
+ db/mongo*
4
+ log/*
5
+ tmp/*
6
+ tmp/**/*
7
+ config/database.yml
8
+ coverage/*
9
+ coverage/**/*
10
+ pkg
data/.loadpath ADDED
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <loadpath>
3
+ <pathentry path="" type="src"/>
4
+ <pathentry path="org.rubypeople.rdt.launching.RUBY_CONTAINER" type="con"/>
5
+ </loadpath>
data/.project ADDED
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>grip</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>org.rubypeople.rdt.core.rubybuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>org.rubypeople.rdt.core.rubynature</nature>
16
+ </natures>
17
+ </projectDescription>
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 John Nunemaker
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,36 @@
1
+ = Grip
2
+
3
+ Attachment plugin for MongoMapper that uses GridFS.
4
+
5
+ == Usage
6
+
7
+ Declare the plugin and use the attachment method to make attachments.
8
+
9
+ class Foo
10
+ include MongoMapper::Document
11
+ plugin Grip
12
+
13
+ attachment :image
14
+ attachment :pdf
15
+ end
16
+
17
+ This gives you #image, #image=, #pdf, and #pdf=. The = methods take any IO that responds to read (File, Tempfile, etc). The image and pdf methods return a GridIO instance (can be found in the ruby driver).
18
+
19
+ == Dependencies
20
+
21
+ * MongoMapper 0.7.1 - gem install mongo_mapper
22
+ * Wand >= 0.2.1 - gem install wand
23
+
24
+ == Note on Patches/Pull Requests
25
+
26
+ * Fork the project.
27
+ * Make your feature addition or bug fix.
28
+ * Add tests for it. This is important so I don't break it in a
29
+ future version unintentionally.
30
+ * Commit, do not mess with rakefile, version, or history.
31
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
32
+ * Send me a pull request. Bonus points for topic branches.
33
+
34
+ == Copyright
35
+
36
+ Copyright (c) 2010 John Nunemaker. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require File.join(File.dirname(__FILE__), 'lib', 'grip', 'version')
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "jonbell-grip"
10
+ gem.summary = %Q{Attachment plugin for MongoMapper that uses GridFS.}
11
+ gem.description = %Q{Attachment plugin for MongoMapper that uses GridFS.}
12
+ gem.email = "nunemaker@gmail.com"
13
+ gem.homepage = "http://github.com/jonbell/grip"
14
+ gem.authors = ["John Nunemaker", "Jonathan Bell"]
15
+ gem.version = Grip::Version
16
+
17
+ gem.add_dependency 'wand', '>= 0.2.1'
18
+ gem.add_development_dependency 'mongo_mapper', '0.7.1'
19
+ gem.add_development_dependency 'shoulda'
20
+ end
21
+ Jeweler::GemcutterTasks.new
22
+ rescue LoadError
23
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
24
+ end
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new(:test) do |test|
28
+ test.libs << 'lib' << 'test'
29
+ test.ruby_opts << '-rubygems'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ begin
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ end
41
+ rescue LoadError
42
+ task :rcov do
43
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
44
+ end
45
+ end
46
+
47
+ task :test => :check_dependencies
48
+
49
+ task :default => :test
50
+
51
+ require 'rake/rdoctask'
52
+ Rake::RDocTask.new do |rdoc|
53
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
54
+
55
+ rdoc.rdoc_dir = 'rdoc'
56
+ rdoc.title = "grip #{version}"
57
+ rdoc.rdoc_files.include('README*')
58
+ rdoc.rdoc_files.include('lib/**/*.rb')
59
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{jonbell-grip}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Nunemaker", "Jonathan Bell"]
12
+ s.date = %q{2010-03-25}
13
+ s.description = %q{Attachment plugin for MongoMapper that uses GridFS.}
14
+ s.email = %q{nunemaker@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".gitignore",
21
+ ".loadpath",
22
+ ".project",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "jonbell-grip.gemspec",
27
+ "lib/grip.rb",
28
+ "lib/grip/version.rb",
29
+ "test/fixtures/azathoth.jpg",
30
+ "test/fixtures/cthulhu.png",
31
+ "test/fixtures/sample.pdf",
32
+ "test/helper.rb",
33
+ "test/test_grip.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/jonbell/grip}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.6}
39
+ s.summary = %q{Attachment plugin for MongoMapper that uses GridFS.}
40
+ s.test_files = [
41
+ "test/helper.rb",
42
+ "test/test_grip.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_runtime_dependency(%q<wand>, [">= 0.2.1"])
51
+ s.add_development_dependency(%q<mongo_mapper>, ["= 0.7.1"])
52
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<wand>, [">= 0.2.1"])
55
+ s.add_dependency(%q<mongo_mapper>, ["= 0.7.1"])
56
+ s.add_dependency(%q<shoulda>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<wand>, [">= 0.2.1"])
60
+ s.add_dependency(%q<mongo_mapper>, ["= 0.7.1"])
61
+ s.add_dependency(%q<shoulda>, [">= 0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,3 @@
1
+ module Grip
2
+ Version = '0.1.1'
3
+ end
data/lib/grip.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'mime/types'
2
+ require 'wand'
3
+
4
+ module Grip
5
+ module ClassMethods
6
+ def attachment(name)
7
+ write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
8
+ attachment_definitions[name] = {}
9
+
10
+ after_save :save_attachments
11
+ before_destroy :destroy_attached_files
12
+
13
+ key "#{name}_id".to_sym, ObjectId
14
+ key "#{name}_name".to_sym, String
15
+ key "#{name}_size".to_sym, Integer
16
+ key "#{name}_type".to_sym, String
17
+
18
+ define_method(name) do
19
+ self.class.grid.get(self["#{name}_id"])
20
+ end
21
+
22
+ define_method("#{name}=") do |file|
23
+ self["#{name}_id"] = Mongo::ObjectID.new
24
+ self["#{name}_size"] = File.size(file)
25
+ self["#{name}_type"] = Wand.wave(file.path)
26
+ self["#{name}_name"] = if file.respond_to?(:original_filename)
27
+ file.original_filename
28
+ else
29
+ File.basename(file.path)
30
+ end
31
+ self.class.attachment_definitions[name] = file
32
+ end
33
+ end
34
+
35
+ def grid
36
+ @grid ||= Mongo::Grid.new(MongoMapper.database)
37
+ end
38
+
39
+ def attachment_definitions
40
+ read_inheritable_attribute(:attachment_definitions)
41
+ end
42
+
43
+ def _root_document
44
+
45
+ end
46
+ end
47
+
48
+ module InstanceMethods
49
+ private
50
+ def save_attachments
51
+ self.class.attachment_definitions.each do |attachment|
52
+ name, file = attachment
53
+ content_type = self["#{name}_type"]
54
+
55
+ if file.respond_to?(:read)
56
+ self.class.grid.put(file.read, self["#{name}_name"], :content_type => content_type, :_id => self["#{name}_id"])
57
+ end
58
+ end
59
+ end
60
+
61
+ def destroy_attached_files
62
+ self.class.attachment_definitions.each do |name, attachment|
63
+ self.class.grid.delete(self["#{name}_id"])
64
+ end
65
+ end
66
+ end
67
+ end
Binary file
Binary file
Binary file
data/test/helper.rb ADDED
@@ -0,0 +1,24 @@
1
+ require 'test/unit'
2
+ require 'tempfile'
3
+ require 'pp'
4
+
5
+ require 'mongo_mapper'
6
+
7
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/grip')
8
+
9
+ MongoMapper.database = "testing"
10
+
11
+ class Test::Unit::TestCase
12
+ def self.test(name, &block)
13
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
14
+ defined = instance_method(test_name) rescue false
15
+ raise "#{test_name} is already defined in #{self}" if defined
16
+ if block_given?
17
+ define_method(test_name, &block)
18
+ else
19
+ define_method(test_name) do
20
+ flunk "No implementation provided for #{name}"
21
+ end
22
+ end
23
+ end
24
+ end
data/test/test_grip.rb ADDED
@@ -0,0 +1,107 @@
1
+ require 'helper'
2
+
3
+ class Foo
4
+ include MongoMapper::Document
5
+ plugin Grip
6
+
7
+ many :bars
8
+
9
+ attachment :image
10
+ attachment :pdf
11
+ end
12
+
13
+ class Bar
14
+ include MongoMapper::EmbeddedDocument
15
+ plugin Grip
16
+
17
+ attachment :file
18
+ end
19
+
20
+ class GripTest < Test::Unit::TestCase
21
+ def setup
22
+ MongoMapper.database.collections.each(&:remove)
23
+ @grid = Mongo::Grid.new(MongoMapper.database)
24
+
25
+ dir = File.dirname(__FILE__) + '/fixtures'
26
+ @pdf = File.open("#{dir}/sample.pdf", 'r')
27
+ @pdf_contents = File.read("#{dir}/sample.pdf")
28
+ @image = File.open("#{dir}/cthulhu.png", 'r')
29
+ @image_contents = File.read("#{dir}/cthulhu.png")
30
+ @file = File.open("#{dir}/azathoth.jpg", 'r')
31
+ @file_contents = File.read("#{dir}/azathoth.jpg")
32
+
33
+ @doc = Foo.create(:image => @image, :pdf => @pdf, :bars => [Bar.new(:file => @file)])
34
+ @doc.reload
35
+ end
36
+
37
+ def teardown
38
+ @pdf.close
39
+ @image.close
40
+ @file.close
41
+ end
42
+
43
+ test "assigns keys correctly" do
44
+ assert_equal 27582, @doc.image_size
45
+ assert_equal 8775, @doc.pdf_size
46
+
47
+ assert_equal "image/png", @doc.image_type
48
+ assert_equal "application/pdf", @doc.pdf_type
49
+ assert_equal "image/jpeg", embedded_doc.file_type
50
+
51
+ assert_not_nil @doc.image_id
52
+ assert_not_nil @doc.pdf_id
53
+ assert_not_nil embedded_doc.file_id
54
+ assert_kind_of Mongo::ObjectID, @doc.image_id
55
+ assert_kind_of Mongo::ObjectID, @doc.pdf_id
56
+ assert_kind_of Mongo::ObjectID, embedded_doc.file_id
57
+
58
+ assert_equal "image/png", @grid.get(@doc.image_id).content_type
59
+ assert_equal "application/pdf", @grid.get(@doc.pdf_id).content_type
60
+ assert_equal "image/jpeg", @grid.get(embedded_doc.file_id).content_type
61
+ end
62
+
63
+ test "assigns file name from path if original file name not available" do
64
+ assert_equal 'cthulhu.png', @doc.image_name
65
+ assert_equal 'sample.pdf', @doc.pdf_name
66
+ assert_equal 'azathoth.jpg', embedded_doc.file_name
67
+ end
68
+
69
+ test "assigns file name from original filename if available" do
70
+ begin
71
+ file = Tempfile.new('testing.txt')
72
+ def file.original_filename
73
+ 'testing.txt'
74
+ end
75
+
76
+ doc = Foo.create(:image => file)
77
+ assert_equal 'testing.txt', doc.image_name
78
+ ensure
79
+ file.close
80
+ end
81
+ end
82
+
83
+ test "responds to keys" do
84
+ [ :pdf_size, :pdf_id, :pdf_name, :pdf_type,
85
+ :image_size, :image_id, :image_name, :image_type
86
+ ].each do |method|
87
+ assert @doc.respond_to?(method)
88
+ end
89
+ end
90
+
91
+ test "saves attachments correctly" do
92
+ assert_equal @pdf_contents, @doc.pdf.read
93
+ assert_equal @image_contents, @doc.image.read
94
+ assert_equal @file_contents, embedded_doc.file.read
95
+ end
96
+
97
+ test "cleans up attachments on destroy" do
98
+ @doc.destroy
99
+ assert_raises(Mongo::GridError) { @grid.get(@doc.image_id) }
100
+ assert_raises(Mongo::GridError) { @grid.get(@doc.pdf_id) }
101
+ assert_raises(Mongo::GridError) { @grid.get(embedded_doc.file_id) }
102
+ end
103
+
104
+ def embedded_doc
105
+ @doc.bars.first
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jonbell-grip
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - John Nunemaker
13
+ - Jonathan Bell
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-03-25 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: wand
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 2
31
+ - 1
32
+ version: 0.2.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: mongo_mapper
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 0
44
+ - 7
45
+ - 1
46
+ version: 0.7.1
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: shoulda
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id003
61
+ description: Attachment plugin for MongoMapper that uses GridFS.
62
+ email: nunemaker@gmail.com
63
+ executables: []
64
+
65
+ extensions: []
66
+
67
+ extra_rdoc_files:
68
+ - LICENSE
69
+ - README.rdoc
70
+ files:
71
+ - .gitignore
72
+ - .loadpath
73
+ - .project
74
+ - LICENSE
75
+ - README.rdoc
76
+ - Rakefile
77
+ - jonbell-grip.gemspec
78
+ - lib/grip.rb
79
+ - lib/grip/version.rb
80
+ - test/fixtures/azathoth.jpg
81
+ - test/fixtures/cthulhu.png
82
+ - test/fixtures/sample.pdf
83
+ - test/helper.rb
84
+ - test/test_grip.rb
85
+ has_rdoc: true
86
+ homepage: http://github.com/jonbell/grip
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options:
91
+ - --charset=UTF-8
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirements: []
109
+
110
+ rubyforge_project:
111
+ rubygems_version: 1.3.6
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Attachment plugin for MongoMapper that uses GridFS.
115
+ test_files:
116
+ - test/helper.rb
117
+ - test/test_grip.rb