Permalink
Browse files

Fix storing raw data

Change-Id: Idd57a242bb2f317994571c1512e627b4c87a7889
Reviewed-on: http://review.couchbase.org/21776
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
  • Loading branch information...
1 parent 6bb168d commit 17d2bb373ceef1581717caa0569ec513dc718727 @avsej avsej committed Oct 19, 2012
Showing with 33 additions and 14 deletions.
  1. +21 −13 lib/couchbase/model.rb
  2. +12 −1 test/test_model.rb
View
@@ -120,6 +120,9 @@ class Model
# @since 0.4.5
attr_reader :errors
+ # @since 0.4.5
+ attr_reader :raw
+
# @private Container for all attributes with defaults of all subclasses
@@attributes = ::Hash.new {|hash, key| hash[key] = {}}
@@ -391,6 +394,7 @@ def self.belongs_to(name, options = {})
def self.find(id)
if id && (res = bucket.get(id, :quiet => false, :extended => true))
obj, flags, cas = res
+ obj = {:raw => obj} unless obj.is_a?(Hash)
new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
end
end
@@ -408,6 +412,7 @@ def self.find(id)
def self.find_by_id(id)
if id && (res = bucket.get(id, :quiet => true, :extended => true))
obj, flags, cas = res
+ obj = {:raw => obj} unless obj.is_a?(Hash)
new({:id => id, :meta => {'flags' => flags, 'cas' => cas}}.merge(obj))
end
end
@@ -431,6 +436,14 @@ def self.create(*args)
# @param [Hash] attrs attribute-value pairs
def initialize(attrs = {})
@errors = ::ActiveModel::Errors.new(self) if defined?(::ActiveModel)
+ @_attributes = ::Hash.new do |h, k|
+ default = self.class.attributes[k]
+ h[k] = if default.respond_to?(:call)
+ default.call
+ else
+ default
+ end
+ end
case attrs
when Hash
if defined?(HashWithIndifferentAccess) && !attrs.is_a?(HashWithIndifferentAccess)
@@ -441,17 +454,10 @@ def initialize(attrs = {})
@value = attrs.delete(:value)
@doc = attrs.delete(:doc)
@meta = attrs.delete(:meta)
- @_attributes = ::Hash.new do |h, k|
- default = self.class.attributes[k]
- h[k] = if default.respond_to?(:call)
- default.call
- else
- default
- end
- end
+ @raw = attrs.delete(:raw)
update_attributes(@doc || attrs)
else
- @_raw = attrs
+ @raw = attrs
end
end
@@ -472,14 +478,15 @@ def create(options = {})
if respond_to?(:valid?) && !valid?
raise Couchbase::Error::RecordInvalid.new(self)
end
- value = @_raw ? @_raw : attributes_with_values
+ options = model.defaults.merge(options)
+ value = (options[:format] == :plain) ? @raw : attributes_with_values
unless @meta
@meta = {}
if @meta.respond_to?(:with_indifferent_access)
@meta = @meta.with_indifferent_access
end
end
- @meta['cas'] = model.bucket.add(@id, value, model.defaults.merge(options))
+ @meta['cas'] = model.bucket.add(@id, value, options)
self
end
@@ -505,8 +512,9 @@ def save(options = {})
if respond_to?(:valid?) && !valid?
raise Couchbase::Error::RecordInvalid.new(self)
end
- value = @_raw ? @_raw : attributes_with_values
- @meta['cas'] = model.bucket.replace(@id, value, model.defaults.merge(options))
+ options = model.defaults.merge(options)
+ value = (options[:format] == :plain) ? @raw : attributes_with_values
+ @meta['cas'] = model.bucket.replace(@id, value, options)
self
end
View
@@ -41,12 +41,16 @@ class Beer < Couchbase::Model
belongs_to :brewery
end
+class Attachment < Couchbase::Model
+ defaults :format => :plain
+end
+
class TestModel < MiniTest::Unit::TestCase
def setup
@mock = start_mock
bucket = Couchbase.connect(:hostname => @mock.host, :port => @mock.port)
- [Post, ValidPost, Brewery, Beer].each do |model|
+ [Post, ValidPost, Brewery, Beer, Attachment].each do |model|
model.bucket = bucket
end
end
@@ -203,4 +207,11 @@ def test_validation
end
end
+ def test_blob_documents
+ contents = File.read(__FILE__)
+ id = Attachment.create(:raw => contents).id
+ blob = Attachment.find(id)
+ assert_equal contents, blob.raw
+ end
+
end

0 comments on commit 17d2bb3

Please sign in to comment.