Class: Castkit::DataObject
- Inherits:
-
Object
- Object
- Castkit::DataObject
- Extended by:
- Castkit::DataObjectExtensions::AttributeTypes, Castkit::DataObjectExtensions::Attributes
- Defined in:
- lib/castkit/data_object.rb
Overview
Base class for defining declarative, typed data transfer objects (DTOs).
Includes typecasting, validation, access control, serialization, deserialization, and support for custom serializers.
Class Method Summary collapse
-
.cast(obj) ⇒ self
Casts a value into an instance of this class.
-
.dump(obj) ⇒ String
Converts an object to its JSON representation.
-
.serializer(value = nil) ⇒ Class<Castkit::Serializer>?
Gets or sets the serializer class to use for instances of this object.
Instance Method Summary collapse
-
#initialize(fields = {}) ⇒ DataObject
constructor
Initializes the DTO from a hash of attributes.
-
#to_hash(visited: nil) ⇒ Hash
(also: #to_h, #serialize)
Serializes the DTO to a Ruby hash.
-
#to_json(options = nil) ⇒ String
Serializes the DTO to a JSON string.
Methods included from Castkit::DataObjectExtensions::Attributes
attribute, attributes, composite, optional, readonly, required, transient, writeonly
Methods included from Castkit::DataObjectExtensions::AttributeTypes
array, boolean, dataobject, date, datetime, float, hash, integer, string, unwrapped
Methods included from Castkit::DataObjectExtensions::Deserialization
Methods included from Castkit::DataObjectExtensions::Config
included, #root_key, #root_key_set?
Constructor Details
#initialize(fields = {}) ⇒ DataObject
Initializes the DTO from a hash of attributes.
79 80 81 82 83 84 85 86 |
# File 'lib/castkit/data_object.rb', line 79 def initialize(fields = {}) root = self.class.root fields = fields[root] if root && fields.key?(root) fields = unwrap_prefixed_fields!(fields) validate_keys!(fields) deserialize_attributes!(fields) end |
Class Method Details
.cast(obj) ⇒ self
Casts a value into an instance of this class.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/castkit/data_object.rb', line 55 def cast(obj) case obj when self obj when Hash from_h(obj) else raise Castkit::DataObjectError, "Can't cast #{obj.class} to #{name}" end end |
.dump(obj) ⇒ String
Converts an object to its JSON representation.
70 71 72 |
# File 'lib/castkit/data_object.rb', line 70 def dump(obj) obj.to_json end |
.serializer(value = nil) ⇒ Class<Castkit::Serializer>?
Gets or sets the serializer class to use for instances of this object.
40 41 42 43 44 45 46 47 48 |
# File 'lib/castkit/data_object.rb', line 40 def serializer(value = nil) if value raise ArgumentError, "Serializer must inherit from Castkit::Serializer" unless value < Castkit::Serializer @serializer = value else @serializer end end |
Instance Method Details
#to_hash(visited: nil) ⇒ Hash Also known as: to_h, serialize
Serializes the DTO to a Ruby hash.
92 93 94 95 |
# File 'lib/castkit/data_object.rb', line 92 def to_hash(visited: nil) serializer = self.class.serializer || Castkit::DefaultSerializer serializer.call(self, visited: visited) end |
#to_json(options = nil) ⇒ String
Serializes the DTO to a JSON string.
101 102 103 |
# File 'lib/castkit/data_object.rb', line 101 def to_json( = nil) JSON.generate(serializer.call(self), ) end |