Class: Acta::Model
- Inherits:
-
Object
- Object
- Acta::Model
- Includes:
- ActiveModel::Attributes, ActiveModel::Model
- Defined in:
- lib/acta/model.rb
Class Method Summary collapse
-
.attribute(name, type = nil, array_of: nil, **options) ⇒ Object
Accept: - a class as a type (Acta::Model / Acta::Serializable) — wrapped in ModelType - array_of: Class or array_of: :symbol — wrapped in ArrayType - standard symbol types (:string, :integer, …) — forwarded to AM.
- .from_acta_hash(hash) ⇒ Object
Instance Method Summary collapse
Class Method Details
.attribute(name, type = nil, array_of: nil, **options) ⇒ Object
Accept:
-
a class as a type (Acta::Model / Acta::Serializable) — wrapped in ModelType
-
array_of: Class or array_of: :symbol — wrapped in ArrayType
-
standard symbol types (:string, :integer, …) — forwarded to AM
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/acta/model.rb', line 17 def self.attribute(name, type = nil, array_of: nil, **) if array_of element = element_type_for(array_of) type = Acta::ArrayType.new(element) elsif type.is_a?(Class) type = Acta::ModelType.new(type) end if type.nil? super(name, **) else super(name, type, **) end end |
.from_acta_hash(hash) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/acta/model.rb', line 47 def self.from_acta_hash(hash) types = attribute_types filtered = hash.each_with_object({}) do |(k, v), acc| key = k.to_s next unless types.key?(key) acc[key.to_sym] = types[key].deserialize(v) end new(**filtered) end |
Instance Method Details
#to_acta_hash ⇒ Object
41 42 43 44 45 |
# File 'lib/acta/model.rb', line 41 def to_acta_hash self.class.attribute_types.each_with_object({}) do |(name, type), hash| hash[name] = type.serialize(public_send(name)) end end |