Class: Castkit::DataObject
- Inherits:
-
Object
- Object
- Castkit::DataObject
- Includes:
- Castkit::DSL::DataObject
- 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
- .build(&block) ⇒ Object
-
.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::Serializers::Base>?
Gets or sets the serializer class to use for instances of this object.
Instance Method Summary collapse
-
#__raw ⇒ Hash{Symbol => Object}
The raw data provided during instantiation.
-
#initialize(data = {}) ⇒ 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.
-
#unknown_attributes ⇒ Hash{Symbol => Object}
Undefined attributes provided during instantiation.
Methods included from Castkit::DSL::DataObject
Constructor Details
#initialize(data = {}) ⇒ DataObject
Initializes the DTO from a hash of attributes.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/castkit/data_object.rb', line 88 def initialize(data = {}) super() cattri_variable_set(:__raw, data.dup.freeze) data = unwrap_root(data) cattri_variable_set(:unknown_attributes, data.reject { |key, _| self.class.attributes.key?(key.to_sym) }.freeze) validate_data!(data) deserialize_attributes!(data) end |
Class Method Details
.build(&block) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/castkit/data_object.rb', line 29 def build(&block) klass = Class.new(self) klass.class_eval(&block) if block_given? klass end |
.cast(obj) ⇒ self
Casts a value into an instance of this class.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/castkit/data_object.rb', line 58 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.
73 74 75 |
# File 'lib/castkit/data_object.rb', line 73 def dump(obj) obj.to_json end |
.serializer(value = nil) ⇒ Class<Castkit::Serializers::Base>?
Gets or sets the serializer class to use for instances of this object.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/castkit/data_object.rb', line 41 def serializer(value = nil) if value unless value < Castkit::Serializers::Base raise ArgumentError, "Serializer must inherit from Castkit::Serializers::Base" end @serializer = value else @serializer end end |
Instance Method Details
#__raw ⇒ Hash{Symbol => Object}
Returns The raw data provided during instantiation.
79 |
# File 'lib/castkit/data_object.rb', line 79 cattri :__raw, nil, expose: :read |
#to_hash(visited: nil) ⇒ Hash Also known as: to_h, serialize
Serializes the DTO to a Ruby hash.
105 106 107 |
# File 'lib/castkit/data_object.rb', line 105 def to_hash(visited: nil) serializer.call(self, visited: visited) end |
#to_json(options = nil) ⇒ String
Serializes the DTO to a JSON string.
113 114 115 |
# File 'lib/castkit/data_object.rb', line 113 def to_json( = nil) JSON.generate(serializer.call(self), ) end |
#unknown_attributes ⇒ Hash{Symbol => Object}
Returns Undefined attributes provided during instantiation.
82 |
# File 'lib/castkit/data_object.rb', line 82 cattri :unknown_attributes, nil, expose: :read |