Browse files

Added Joint::IO for stuffing random things in grid fs.

  • Loading branch information...
1 parent d27ff92 commit edaeeba08dc038bceb17dbd203bd0dcdced03dfe @jnunemaker committed Dec 30, 2010
Showing with 62 additions and 0 deletions.
  1. +1 −0 lib/joint.rb
  2. +14 −0 lib/joint/io.rb
  3. +28 −0 test/joint/test_io.rb
  4. +19 −0 test/test_joint.rb
View
1 lib/joint.rb
@@ -37,3 +37,4 @@ def self.type(file)
require 'joint/class_methods'
require 'joint/instance_methods'
require 'joint/attachment_proxy'
+require 'joint/io'
View
14 lib/joint/io.rb
@@ -0,0 +1,14 @@
+module Joint
+ class IO
+ attr_accessor :name, :content, :content_type, :size
+
+ def initialize(attrs={})
+ attrs.each { |key, value| send("#{key}=", value) }
+ @content_type ||= 'plain/text'
+ @size ||= @content.size unless @content.nil?
+ end
+
+ alias path name
+ alias read content
+ end
+end
View
28 test/joint/test_io.rb
@@ -0,0 +1,28 @@
+require 'helper'
+
+class IOTest < Test::Unit::TestCase
+ context "#initialize" do
+ should "set attributes from hash" do
+ Joint::IO.new(:name => 'foo').name.should == 'foo'
+ end
+ end
+
+ should "default content_type to plain text" do
+ Joint::IO.new.content_type.should == 'plain/text'
+ end
+
+ should "default size to content size" do
+ content = 'This is my content'
+ Joint::IO.new(:content => content).size.should == content.size
+ end
+
+ should "alias path to name" do
+ Joint::IO.new(:name => 'foo').path.should == 'foo'
+ end
+
+ context "#read" do
+ should "return content" do
+ Joint::IO.new(:content => 'Testing').read.should == 'Testing'
+ end
+ end
+end
View
19 test/test_joint.rb
@@ -342,4 +342,23 @@ def @image.original_filename
model.should be_valid
end
end
+
+ context "Assigning joint io instance" do
+ setup do
+ io = Joint::IO.new({
+ :name => 'foo.txt',
+ :content_type => 'plain/text',
+ :content => 'This is my stuff',
+ :size => 19,
+ })
+ @asset = Asset.create(:file => io)
+ end
+
+ should "work" do
+ @asset.file_name.should == 'foo.txt'
+ @asset.file_size.should == 19
+ @asset.file_type.should == 'plain/text'
+ @asset.file.read.should == 'This is my stuff'
+ end
+ end
end

0 comments on commit edaeeba

Please sign in to comment.