Class: Acta::Types::Model

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/acta/types/model.rb

Overview

Wraps an Acta::Model subclass (or any class with ‘to_acta_hash` / `from_acta_hash`, e.g. AR classes that include Acta::Serializable) so it can be used as an `attribute` type on another Acta::Model. The wrapping is automatic — `attribute :location, GeoPoint` invokes this internally; consumers don’t construct it directly.

Instance Method Summary collapse

Constructor Details

#initialize(wrapped_class) ⇒ Model

Returns a new instance of Model.



13
14
15
16
# File 'lib/acta/types/model.rb', line 13

def initialize(wrapped_class)
  super()
  @wrapped_class = wrapped_class
end

Instance Method Details

#cast(value) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/acta/types/model.rb', line 18

def cast(value)
  case value
  when nil then nil
  when @wrapped_class then value
  when Hash then @wrapped_class.from_acta_hash(value)
  else
    raise ArgumentError, "Cannot cast #{value.class} (#{value.inspect}) to #{@wrapped_class}"
  end
end

#deserialize(value) ⇒ Object



34
35
36
# File 'lib/acta/types/model.rb', line 34

def deserialize(value)
  cast(value)
end

#serialize(value) ⇒ Object



28
29
30
31
32
# File 'lib/acta/types/model.rb', line 28

def serialize(value)
  return nil if value.nil?

  value.to_acta_hash
end