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.
18 19 20 21 22 23 24 |
# File 'lib/couchbase-orm/types/nested.rb', line 18 def initialize(type:) raise ArgumentError, "type is nil" if type.nil? raise ArgumentError, "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.
16 17 18 |
# File 'lib/couchbase-orm/types/nested.rb', line 16 def model_class @model_class end |
Instance Method Details
#cast(value) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/couchbase-orm/types/nested.rb', line 26 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, "Nested: #{value.inspect} (#{value.class}) is not supported for cast" end |
#serialize(value) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/couchbase-orm/types/nested.rb', line 34 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, "Nested: #{value.inspect} (#{value.class}) is not supported for serialization" end |