Skip to content

Commit

Permalink
Merge pull request #83 from Mapotempo/add_type_hash
Browse files Browse the repository at this point in the history
add Hash types
  • Loading branch information
giallon authored May 28, 2024
2 parents 6c59e64 + 1b2e41a commit cff1218
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/couchbase-orm/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "couchbase-orm/types/array"
require "couchbase-orm/types/nested"
require "couchbase-orm/types/encrypted"
require "couchbase-orm/types/hash"

if ActiveModel::VERSION::MAJOR <= 6
# In Rails 5, the type system cannot allow overriding the default types
Expand All @@ -18,3 +19,5 @@
ActiveModel::Type.register(:array, CouchbaseOrm::Types::Array)
ActiveModel::Type.register(:nested, CouchbaseOrm::Types::Nested)
ActiveModel::Type.register(:encrypted, CouchbaseOrm::Types::Encrypted)
ActiveModel::Type.register(:hash, CouchbaseOrm::Types::Hash)

21 changes: 21 additions & 0 deletions lib/couchbase-orm/types/hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module CouchbaseOrm
module Types
class Hash < ActiveModel::Type::Value
def cast(value)
return nil if value.nil?
return value if value.is_a?(ActiveSupport::HashWithIndifferentAccess)
return value.with_indifferent_access if value.is_a?(::Hash)

raise ArgumentError, "Hash: #{value.inspect} (#{value.class}) is not supported for cast"
end

def serialize(value)
return nil if value.nil?
return value.as_json if value.is_a?(ActiveSupport::HashWithIndifferentAccess)
return value.with_indifferent_access.as_json if value.is_a?(::Hash)

raise ArgumentError, "Hash: #{value.inspect} (#{value.class}) is not supported for serialize"
end
end
end
end
4 changes: 2 additions & 2 deletions lib/couchbase-orm/types/nested.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def initialize(type:)
def cast(value)
return nil if value.nil?
return value if value.is_a?(@model_class)
return @model_class.new(value) if value.is_a?(Hash)
return @model_class.new(value) if value.is_a?(::Hash)

raise ArgumentError, "Nested: #{value.inspect} (#{value.class}) is not supported for cast"
end

def serialize(value)
return nil if value.nil?
value = @model_class.new(value) if value.is_a?(Hash)
value = @model_class.new(value) if value.is_a?(::Hash)
return value.send(:serialized_attributes) if value.is_a?(@model_class)

raise ArgumentError, "Nested: #{value.inspect} (#{value.class}) is not supported for serialization"
Expand Down
27 changes: 27 additions & 0 deletions spec/type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class N1qlTypeTest < CouchbaseOrm::Base
attribute :some_time, :timestamp
attribute :precision3_time, :datetime3decimal
attribute :active, :boolean
attribute :address, :hash

index_n1ql :name, validate: false
index_n1ql :age, validate: false
Expand Down Expand Up @@ -315,3 +316,29 @@ class N1qlTypeTest < CouchbaseOrm::Base
end
end
end

describe CouchbaseOrm::Types::Hash do
it 'should cast Hash to HashWithIndifferentAccess' do
expect(CouchbaseOrm::Types::Hash.new.cast({'a' => 1}).class).to be(HashWithIndifferentAccess)
end

it 'should cast nil to nil' do
expect(CouchbaseOrm::Types::Hash.new.cast(nil)).to be_nil
end

it 'should cast HashWithIndifferentAccess to HashWithIndifferentAccess' do
expect(CouchbaseOrm::Types::Hash.new.cast({'a' => 1}.with_indifferent_access).class).to be(HashWithIndifferentAccess)
end

it 'should serialize Hash as json hash' do
expect(CouchbaseOrm::Types::Hash.new.serialize({'a' => 1}).class).to be(Hash)
end

it 'should serialize HashWithIndifferentAccess as json hash' do
expect(CouchbaseOrm::Types::Hash.new.serialize({'a' => 1}.with_indifferent_access).class).to be(Hash)
end

it 'should serialize nil as nil' do
expect(CouchbaseOrm::Types::Hash.new.serialize(nil)).to be_nil
end
end

0 comments on commit cff1218

Please sign in to comment.