Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Hash types #83

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading