Class: CouchbaseOrm::Types::Nested
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- CouchbaseOrm::Types::Nested
- Defined in:
- lib/couchbase-orm/types/nested.rb
Instance Attribute Summary collapse
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
Instance Method Summary collapse
- #cast(value) ⇒ Object
-
#initialize(type:) ⇒ Nested
constructor
A new instance of Nested.
- #serialize(value) ⇒ Object
Constructor Details
#initialize(type:) ⇒ Nested
Returns a new instance of Nested.
19 20 21 22 23 24 25 |
# File 'lib/couchbase-orm/types/nested.rb', line 19 def initialize(type:) raise ArgumentError.new('type is nil') if type.nil? raise ArgumentError.new("type is not a class : #{type.inspect}") unless type.is_a?(Class) @model_class = type super() end |
Instance Attribute Details
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
17 18 19 |
# File 'lib/couchbase-orm/types/nested.rb', line 17 def model_class @model_class end |
Instance Method Details
#cast(value) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/couchbase-orm/types/nested.rb', line 27 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) raise ArgumentError.new("Nested: #{value.inspect} (#{value.class}) is not supported for cast") end |
#serialize(value) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/couchbase-orm/types/nested.rb', line 35 def serialize(value) return nil if value.nil? value = @model_class.new(value) if value.is_a?(::Hash) return value.send(:serialized_attributes) if value.is_a?(@model_class) raise ArgumentError.new("Nested: #{value.inspect} (#{value.class}) is not supported for serialization") end |