jnunemaker-grip 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/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .DS_Store
2
+ **/.DS_Store
3
+ db/mongo*
4
+ log/*
5
+ tmp/*
6
+ tmp/**/*
7
+ config/database.yml
8
+ coverage/*
9
+ coverage/**/*
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 = "jnunemaker-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/jnunemaker/grip"
14
+ gem.authors = ["John Nunemaker"]
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,61 @@
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{jnunemaker-grip}
8
+ s.version = "0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Nunemaker"]
12
+ s.date = %q{2010-03-10}
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
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "jnunemaker-grip.gemspec",
25
+ "lib/grip.rb",
26
+ "lib/grip/version.rb",
27
+ "test/fixtures/cthulhu.png",
28
+ "test/fixtures/sample.pdf",
29
+ "test/helper.rb",
30
+ "test/test_grip.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/jnunemaker/grip}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{Attachment plugin for MongoMapper that uses GridFS.}
37
+ s.test_files = [
38
+ "test/helper.rb",
39
+ "test/test_grip.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<wand>, [">= 0.2.1"])
48
+ s.add_development_dependency(%q<mongo_mapper>, ["= 0.7.1"])
49
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<wand>, [">= 0.2.1"])
52
+ s.add_dependency(%q<mongo_mapper>, ["= 0.7.1"])
53
+ s.add_dependency(%q<shoulda>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<wand>, [">= 0.2.1"])
57
+ s.add_dependency(%q<mongo_mapper>, ["= 0.7.1"])
58
+ s.add_dependency(%q<shoulda>, [">= 0"])
59
+ end
60
+ end
61
+
@@ -0,0 +1,3 @@
1
+ module Grip
2
+ Version = '0.1'
3
+ end
data/lib/grip.rb ADDED
@@ -0,0 +1,59 @@
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}_name"] = File.basename(file.path)
26
+ self["#{name}_type"] = Wand.wave(file.path)
27
+ self.class.attachment_definitions[name] = file
28
+ end
29
+ end
30
+
31
+ def grid
32
+ @grid ||= Mongo::Grid.new(database)
33
+ end
34
+
35
+ def attachment_definitions
36
+ read_inheritable_attribute(:attachment_definitions)
37
+ end
38
+ end
39
+
40
+ module InstanceMethods
41
+ private
42
+ def save_attachments
43
+ self.class.attachment_definitions.each do |attachment|
44
+ name, file = attachment
45
+ content_type = self["#{name}_type"]
46
+
47
+ if file.respond_to?(:read)
48
+ self.class.grid.put(file.read, self["#{name}_name"], :content_type => content_type, :_id => self["#{name}_id"])
49
+ end
50
+ end
51
+ end
52
+
53
+ def destroy_attached_files
54
+ self.class.attachment_definitions.each do |name, attachment|
55
+ self.class.grid.delete(self["#{name}_id"])
56
+ end
57
+ end
58
+ end
59
+ end
Binary file
Binary file
data/test/helper.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'test/unit'
2
+ require 'pp'
3
+
4
+ require 'mongo_mapper'
5
+
6
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/grip')
7
+
8
+ MongoMapper.database = "testing"
9
+
10
+ class Test::Unit::TestCase
11
+ def self.test(name, &block)
12
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
13
+ defined = instance_method(test_name) rescue false
14
+ raise "#{test_name} is already defined in #{self}" if defined
15
+ if block_given?
16
+ define_method(test_name, &block)
17
+ else
18
+ define_method(test_name) do
19
+ flunk "No implementation provided for #{name}"
20
+ end
21
+ end
22
+ end
23
+ end
data/test/test_grip.rb ADDED
@@ -0,0 +1,68 @@
1
+ require 'helper'
2
+
3
+ class Foo
4
+ include MongoMapper::Document
5
+ plugin Grip
6
+
7
+ attachment :image
8
+ attachment :pdf
9
+ end
10
+
11
+ class GripTest < Test::Unit::TestCase
12
+ def setup
13
+ MongoMapper.database.collections.each(&:remove)
14
+ @grid = Mongo::Grid.new(MongoMapper.database)
15
+
16
+ dir = File.dirname(__FILE__) + '/fixtures'
17
+ @pdf = File.open("#{dir}/sample.pdf", 'r')
18
+ @pdf_contents = File.read("#{dir}/sample.pdf")
19
+ @image = File.open("#{dir}/cthulhu.png", 'r')
20
+ @image_contents = File.read("#{dir}/cthulhu.png")
21
+
22
+ @doc = Foo.create(:image => @image, :pdf => @pdf)
23
+ @doc.reload
24
+ end
25
+
26
+ def teardown
27
+ @pdf.close
28
+ @image.close
29
+ end
30
+
31
+ test "assigns keys correctly" do
32
+ assert_equal 27582, @doc.image_size
33
+ assert_equal 8775, @doc.pdf_size
34
+
35
+ assert_equal 'cthulhu.png', @doc.image_name
36
+ assert_equal 'sample.pdf', @doc.pdf_name
37
+
38
+ assert_equal "image/png", @doc.image_type
39
+ assert_equal "application/pdf", @doc.pdf_type
40
+
41
+ assert_not_nil @doc.image_id
42
+ assert_not_nil @doc.pdf_id
43
+ assert_kind_of Mongo::ObjectID, @doc.image_id
44
+ assert_kind_of Mongo::ObjectID, @doc.pdf_id
45
+
46
+ assert_equal "image/png", @grid.get(@doc.image_id).content_type
47
+ assert_equal "application/pdf", @grid.get(@doc.pdf_id).content_type
48
+ end
49
+
50
+ test "responds to keys" do
51
+ [ :pdf_size, :pdf_id, :pdf_name, :pdf_type,
52
+ :image_size, :image_id, :image_name, :image_type
53
+ ].each do |method|
54
+ assert @doc.respond_to?(method)
55
+ end
56
+ end
57
+
58
+ test "saves attachments correctly" do
59
+ assert_equal @pdf_contents, @doc.pdf.read
60
+ assert_equal @image_contents, @doc.image.read
61
+ end
62
+
63
+ test "cleans up attachments on destroy" do
64
+ @doc.destroy
65
+ assert_raises(Mongo::GridError) { @grid.get(@doc.image_id) }
66
+ assert_raises(Mongo::GridError) { @grid.get(@doc.pdf_id) }
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jnunemaker-grip
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - John Nunemaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-03-10 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: wand
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.1
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mongo_mapper
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.1
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: shoulda
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: Attachment plugin for MongoMapper that uses GridFS.
46
+ email: nunemaker@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .gitignore
56
+ - LICENSE
57
+ - README.rdoc
58
+ - Rakefile
59
+ - jnunemaker-grip.gemspec
60
+ - lib/grip.rb
61
+ - lib/grip/version.rb
62
+ - test/fixtures/cthulhu.png
63
+ - test/fixtures/sample.pdf
64
+ - test/helper.rb
65
+ - test/test_grip.rb
66
+ has_rdoc: true
67
+ homepage: http://github.com/jnunemaker/grip
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options:
72
+ - --charset=UTF-8
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.5
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Attachment plugin for MongoMapper that uses GridFS.
94
+ test_files:
95
+ - test/helper.rb
96
+ - test/test_grip.rb