Module: Castkit::Core::Attributes

Included in:
DataObject
Defined in:
lib/castkit/core/attributes.rb

Overview

Provides DSL and implementation for declaring attributes within a Castkit::DataObject.

Includes support for regular, composite, transient, readonly/writeonly, and grouped attribute definitions.

Instance Method Summary collapse

Instance Method Details

#attribute(field, type, **options) ⇒ void

This method returns an undefined value.

Declares an attribute with the given type and options.

If ‘:transient` is true, defines only standard accessors and skips serialization logic.

Raises:



18
19
20
21
22
23
24
25
26
# File 'lib/castkit/core/attributes.rb', line 18

def attribute(field, type, **options)
  field = field.to_sym
  raise Castkit::DataObjectError, "Attribute '#{field}' already defined" if attributes.key?(field)

  options = build_options(options)
  return define_attribute(field, type, **options) unless options[:transient]

  attr_accessor field
end

#attributesHash{Symbol => Castkit::Attribute}

Returns all declared non-transient attributes.



93
94
95
# File 'lib/castkit/core/attributes.rb', line 93

def attributes
  @attributes ||= {}
end

#composite(field, type, **options, &block) ⇒ Object Also known as: property

Declares a computed (composite) attribute.

The provided block defines the read behavior.

Yield Returns:

  • (Object)

    evaluated composite value



36
37
38
39
# File 'lib/castkit/core/attributes.rb', line 36

def composite(field, type, **options, &block)
  attribute(field, type, **options, composite: true)
  define_method(field, &block)
end

#optional(**options) { ... } ⇒ void

This method returns an undefined value.

Declares a group of optional attributes within the given block.

Yields:

  • defines attributes with ‘required: false`



86
87
88
# File 'lib/castkit/core/attributes.rb', line 86

def optional(**options, &block)
  with_required(false, options, &block)
end

#readonly(**options) { ... } ⇒ void

This method returns an undefined value.

Declares a group of readonly attributes within the given block.

Yields:

  • defines attributes with ‘access: [:read]`



59
60
61
# File 'lib/castkit/core/attributes.rb', line 59

def readonly(**options, &block)
  with_access([:read], options, &block)
end

#required(**options) { ... } ⇒ void

This method returns an undefined value.

Declares a group of required attributes within the given block.

Yields:

  • defines attributes with ‘required: true`



77
78
79
# File 'lib/castkit/core/attributes.rb', line 77

def required(**options, &block)
  with_required(true, options, &block)
end

#transient { ... } ⇒ void

This method returns an undefined value.

Declares a group of transient attributes within the given block.

These attributes are not serialized or included in ‘to_h`.

Yields:

  • defines one or more transient attributes via ‘attribute`



47
48
49
50
51
52
# File 'lib/castkit/core/attributes.rb', line 47

def transient(&block)
  @__transient_context = true
  instance_eval(&block)
ensure
  @__transient_context = nil
end

#writeonly(**options) { ... } ⇒ void

This method returns an undefined value.

Declares a group of writeonly attributes within the given block.

Yields:

  • defines attributes with ‘access: [:write]`



68
69
70
# File 'lib/castkit/core/attributes.rb', line 68

def writeonly(**options, &block)
  with_access([:write], options, &block)
end