grip 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
- *.gem
1
+ *.gem
2
+ pkg/*
@@ -1,9 +1,15 @@
1
- Grip
2
- ==============
1
+ = grip
3
2
 
4
- GridFS Attachment gem for MongoMapper
3
+ GridFS attachments for MongoMapper
5
4
 
6
- **Sample Class**
5
+ = installation
6
+
7
+ The grip gem is hosted on gemcutter.org:
8
+
9
+ * gem install grip
10
+
11
+
12
+ = Usage
7
13
 
8
14
  class Foo
9
15
  include MongoMapper::Document
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |g|
9
+ g.name = 'grip'
10
+ g.summary = %(GridFS attachments for MongoMapper)
11
+ g.description = %(GridFS attachments for MongoMapper)
12
+ g.email = 'signalstatic@gmail.com'
13
+ g.homepage = 'http://github.com/twoism/grip'
14
+ g.authors = %w(twoism)
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts 'Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com'
19
+ end
20
+
21
+ Rake::TestTask.new do |t|
22
+ t.libs = %w(test)
23
+ t.pattern = 'test/**/*_test.rb'
24
+ end
25
+
26
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.4.0
@@ -0,0 +1,52 @@
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{grip}
8
+ s.version = "0.4.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["twoism"]
12
+ s.date = %q{2009-12-23}
13
+ s.description = %q{GridFS attachments for MongoMapper}
14
+ s.email = %q{signalstatic@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
+ "VERSION",
25
+ "grip.gemspec",
26
+ "lib/grip.rb",
27
+ "test/fixtures/cthulhu.png",
28
+ "test/fixtures/sample.pdf",
29
+ "test/test_grip.rb",
30
+ "test/test_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/twoism/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{GridFS attachments for MongoMapper}
37
+ s.test_files = [
38
+ "test/test_grip.rb",
39
+ "test/test_helper.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
+ else
48
+ end
49
+ else
50
+ end
51
+ end
52
+
@@ -1,76 +1,74 @@
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
1
+ require 'mongo/gridfs'
2
+ require 'mime/types'
3
+ require 'tempfile'
4
+
5
+ # if thumbnailable?
6
+ # tmp = Tempfile.new("thumb_#{filename}")
7
+ # MojoMagick::resize(uploaded_file.path, tmp.path, {:width => 50, :height => 40, :scale => '>'})
8
+ # self.thumbnail = tmp.read
9
+ # end
41
10
 
11
+ # open : db, name, mode, options (:root, :metadata, :content_type)
12
+ # read : db, name, length, offset
13
+ # unlink : db, names
14
+ # list : db, root collection
15
+ #
16
+ # GridStore.open(database, 'filename', 'w') { |f|
17
+ # f.puts "Hello, world!"
18
+ # }
19
+
20
+ module Grip
21
+ def self.included(base)
22
+ base.extend Grip::ClassMethods
42
23
  end
24
+
43
25
  module ClassMethods
44
-
45
- def has_grid_attachment name
46
- include InstanceMethods
47
-
48
- # thanks to thoughtbot's paperclip!
26
+ def has_grid_attachment(name)
49
27
  write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
50
28
  attachment_definitions[name] = {}
51
29
 
52
- define_method name do
53
- file_from_grid name
30
+ after_save :save_attachments
31
+ before_destroy :destroy_attached_files
32
+
33
+ key "#{name}_size".to_sym, Integer
34
+ key "#{name}_path".to_sym, String
35
+ key "#{name}_name".to_sym, String
36
+ key "#{name}_content_type".to_sym, String
37
+
38
+ define_method(name) do
39
+ GridFS::GridStore.read(self.class.database, self["#{name}_path"])
54
40
  end
55
-
56
- define_method "#{name}=" do |file|
41
+
42
+ define_method("#{name}=") do |file|
43
+ self['_id'] = Mongo::ObjectID.new if _id.blank?
44
+ self["#{name}_size"] = File.size(file)
45
+ self["#{name}_name"] = File.basename(file.path)
46
+ self["#{name}_path"] = "#{self.class.to_s.underscore}/#{name}/#{_id}"
47
+ self["#{name}_content_type"] = file.content_type rescue MIME::Types.type_for(self["#{name}_name"]).to_s
57
48
  self.class.attachment_definitions[name] = file
58
49
  end
59
-
60
- key "#{name}_path".to_sym, String
61
50
  end
62
51
 
63
52
  def attachment_definitions
64
53
  read_inheritable_attribute(:attachment_definitions)
65
54
  end
66
-
67
55
  end
68
56
 
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
57
+ def save_attachments
58
+ self.class.attachment_definitions.each do |attachment|
59
+ name, file = attachment
60
+
61
+ if (file.is_a?(File) || file.is_a?(Tempfile))
62
+ GridFS::GridStore.open(self.class.database, self["#{name}_path"], 'w', :content_type => self["#{name}_content_type"]) do |f|
63
+ f.write(file.read)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ def destroy_attached_files
70
+ self.class.attachment_definitions.each do |name, attachment|
71
+ GridFS::GridStore.unlink(self.class.database, self["#{name}_path"])
74
72
  end
75
73
  end
76
74
  end
File without changes
@@ -1,58 +1,73 @@
1
1
  require "test_helper"
2
- require File.expand_path(File.dirname(__FILE__) + '/../lib/grip')
3
-
4
2
 
5
3
  class Foo
6
4
  include MongoMapper::Document
7
5
  include Grip
8
- key :name, String
6
+
9
7
  has_grid_attachment :image
10
8
  has_grid_attachment :pdf
11
9
  end
12
10
 
13
-
14
- class TestContent< Test::Unit::TestCase
15
-
16
- context "A MongoMapper Document" do
11
+ class GripTest < Test::Unit::TestCase
12
+ def setup
13
+ MongoMapper.connection.drop_database "test-attachments"
14
+ MongoMapper.database = "test-attachments"
17
15
 
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
16
+ dir = File.dirname(__FILE__) + '/fixtures'
17
+ @pdf = File.open("#{dir}/sample.pdf", 'r')
18
+ @image = File.open("#{dir}/cthulhu.png", 'r')
19
+
20
+ @doc = Foo.create(:image => @image, :pdf => @pdf)
21
+ @doc.reload
22
+ end
49
23
 
50
- @document.destroy
24
+ def teardown
25
+ @pdf.close
26
+ @image.close
27
+ end
51
28
 
52
- assert !GridStore.exist?(MONGO_DB,img_path)
53
- assert !GridStore.exist?(MONGO_DB,pdf_path)
54
- end
55
-
29
+ test "assigns keys correctly" do
30
+ assert_equal 27582, @doc.image_size
31
+ assert_equal 8775, @doc.pdf_size
32
+
33
+ assert_equal 'cthulhu.png', @doc.image_name
34
+ assert_equal 'sample.pdf', @doc.pdf_name
35
+
36
+ assert_equal "image/png", @doc.image_content_type
37
+ assert_equal "application/pdf", @doc.pdf_content_type
38
+
39
+ assert_equal "foo/image/#{@doc.id}", @doc.image_path
40
+ assert_equal "foo/pdf/#{@doc.id}", @doc.pdf_path
41
+
42
+ collection = MongoMapper.database['fs.files']
43
+
44
+ assert_equal "image/png", collection.find_one(:filename => @doc.image_path)['contentType']
45
+ assert_equal "application/pdf", collection.find_one(:filename => @doc.pdf_path)['contentType']
46
+ end
47
+
48
+ test "responds to dynamic keys" do
49
+ [ :pdf_size, :pdf_path, :pdf_name, :pdf_content_type,
50
+ :image_size, :image_path, :image_name, :image_content_type
51
+ ].each do |method|
52
+ assert @doc.respond_to?(method)
56
53
  end
57
54
  end
55
+
56
+ test "saves attachments correctly" do
57
+ assert_equal @image.read, @doc.image
58
+ assert_equal @pdf.read, @doc.pdf
59
+
60
+ assert GridFS::GridStore.exist?(MongoMapper.database, @doc.image_path)
61
+ assert GridFS::GridStore.exist?(MongoMapper.database, @doc.pdf_path)
62
+ end
63
+
64
+ test "cleans up attachments on destroy" do
65
+ assert GridFS::GridStore.exist?(MongoMapper.database, @doc.image_path)
66
+ assert GridFS::GridStore.exist?(MongoMapper.database, @doc.pdf_path)
67
+
68
+ @doc.destroy
69
+
70
+ assert ! GridFS::GridStore.exist?(MongoMapper.database, @doc.image_path)
71
+ assert ! GridFS::GridStore.exist?(MongoMapper.database, @doc.pdf_path)
72
+ end
58
73
  end
@@ -1,25 +1,23 @@
1
- %w{rubygems mongomapper shoulda mongo/gridfs mime/types}.each {|f| require f}
1
+ require 'test/unit'
2
+ require 'pp'
2
3
 
4
+ require 'mongo_mapper'
3
5
 
6
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/grip')
4
7
 
5
8
  MongoMapper.database = "test-attachments"
6
9
 
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
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
23
21
  end
24
22
  end
25
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - twoism
@@ -25,12 +25,15 @@ extra_rdoc_files:
25
25
  files:
26
26
  - .gitignore
27
27
  - LICENSE
28
+ - README.rdoc
29
+ - Rakefile
30
+ - VERSION
31
+ - grip.gemspec
28
32
  - lib/grip.rb
29
- - test/cthulhu.png
30
- - test/sample.pdf
33
+ - test/fixtures/cthulhu.png
34
+ - test/fixtures/sample.pdf
31
35
  - test/test_grip.rb
32
36
  - test/test_helper.rb
33
- - README.rdoc
34
37
  has_rdoc: true
35
38
  homepage: http://github.com/twoism/grip
36
39
  licenses: []