Class: Castkit::DataObject

Overview

Base class for defining declarative, typed data transfer objects (DTOs).

Includes typecasting, validation, access control, serialization, deserialization, and support for custom serializers.

Examples:

Defining a DTO

class UserDto < Castkit::DataObject
  string :name
  integer :age, required: false
end

Instantiating and serializing

user = UserDto.new(name: "Alice", age: 30)
user.to_json #=> '{"name":"Alice","age":30}'

Class Method Summary collapse

Instance Method Summary collapse

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

included

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.

Parameters:

  • fields (Hash) (defaults to: {})

    raw input hash

Raises:



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.

Parameters:

  • obj (self, Hash)

Returns:

  • (self)

Raises:



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.

Parameters:

Returns:

  • (String)


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.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if value does not inherit from Castkit::Serializer



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.

Parameters:

  • visited (Set, nil) (defaults to: nil)

    used to track circular references

Returns:

  • (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.

Parameters:

  • options (Hash, nil) (defaults to: nil)

    options passed to ‘JSON.generate`

Returns:

  • (String)


101
102
103
# File 'lib/castkit/data_object.rb', line 101

def to_json(options = nil)
  JSON.generate(serializer.call(self), options)
end