Class: Castkit::DataObject

Inherits:
Object
  • Object
show all
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.

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::DSL::DataObject

included

Constructor Details

#initialize(data = {}) ⇒ DataObject

Initializes the DTO from a hash of attributes.

Parameters:

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

    raw input hash

Raises:



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.

Parameters:

  • obj (self, Hash)

Returns:

  • (self)

Raises:



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.

Parameters:

Returns:

  • (String)


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.

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if value does not inherit from Castkit::Serializers::Base



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

#__rawHash{Symbol => Object}

Returns The raw data provided during instantiation.

Returns:

  • (Hash{Symbol => Object})

    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.

Parameters:

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

    used to track circular references

Returns:

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

Parameters:

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

    options passed to ‘JSON.generate`

Returns:

  • (String)


113
114
115
# File 'lib/castkit/data_object.rb', line 113

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

#unknown_attributesHash{Symbol => Object}

Returns Undefined attributes provided during instantiation.

Returns:

  • (Hash{Symbol => Object})

    Undefined attributes provided during instantiation.



82
# File 'lib/castkit/data_object.rb', line 82

cattri :unknown_attributes, nil, expose: :read