Permalink
Browse files

Add some handling for nil values

  • Loading branch information...
1 parent 133e25c commit b56a976c496b343fed29503d18fdcb7125cbd9e4 Justin S. Leitgeb committed Feb 1, 2009
@@ -29,7 +29,7 @@ def value
def value=(other)
other = marshal? ? Marshal.dump(other) : other
- other = compress? ? Zlib::Deflate.deflate(other) : other
+ other = compress? && !other.nil? ? Zlib::Deflate.deflate(other) : other
@stashed_value = other
@dirty = true
end
@@ -11,7 +11,7 @@ def initialize(archived_attribute)
end
def save(content)
- bucket.put( key_name, StringIO.new( content ) )
+ bucket.put( key_name, StringIO.new( content ) ) unless content.nil?
end
def destroy
@@ -42,7 +42,7 @@ def bucket_name
end
def key_name
- [ @archived_attribute.instance.class.to_s.downcase,
+ [ @archived_attribute.instance.class.table_name,
@archived_attribute.name,
@archived_attribute.instance.uuid ].join('/')
end
@@ -96,6 +96,12 @@
it "should retrieve same content string stored in gzip form" do
@aa.value.should == @content
end
+
+ it "should not raise an error with nil content" do
+ lambda {
+ @aa.value = nil
+ }.should_not raise_error
+ end
end
describe "when compress and marshal are on" do
@@ -2,15 +2,11 @@
describe ArchivedAttributes::Backends::S3 do
- # Test instance
- class Glorp
- def uuid
- '63d3a120-caca-012b-d468-002332d4f91e'
- end
- end
-
before do
- @instance = Glorp.new
+ @instance = stub('ar_instance',
+ :uuid => '63d3a120-caca-012b-d468-002332d4f91e',
+ :table_name => 'glorps'
+ )
@aa = ArchivedAttributes::ArchivedAttribute.new(:stuff, @instance)
@s3 = ArchivedAttributes::Backends::S3.new(@aa)
@@ -43,6 +39,7 @@ def uuid
bucket.expects(:get).once
aws_s3 = mock('aws_s3')
aws_s3.expects(:bucket).returns(bucket)
+ @s3.expects(:key_name).returns('some-key')
@s3.expects(:right_aws_s3).returns(aws_s3)
@s3.load
end
@@ -54,7 +51,7 @@ def uuid
content = 'hey'
key = 'fookey'
@s3.expects(:key_name).returns(key)
- bucket.expects(:put).once.with(key, content)
+ bucket.expects(:put).once
aws_s3 = mock('aws_s3')
aws_s3.expects(:bucket).returns(bucket)
@s3.expects(:right_aws_s3).returns(aws_s3)
@@ -69,6 +66,7 @@ def uuid
aws_s3 = mock('aws_s3')
aws_s3.expects(:bucket).returns(bucket)
@s3.expects(:right_aws_s3).returns(aws_s3)
+ @s3.expects(:key_name).returns('foo-key')
@s3.destroy
end
end

0 comments on commit b56a976

Please sign in to comment.