Module: Castkit::Ext::Attribute::Options

Included in:
Attribute
Defined in:
lib/castkit/ext/attribute/options.rb

Overview

Provides access to normalized attribute options and helper predicates.

These methods support Castkit attribute behavior such as default values, key mapping, optionality, and structural roles (e.g. composite or unwrapped).

Constant Summary collapse

DEFAULT_OPTIONS =

Default options for attributes.

Returns:

  • (Hash{Symbol => Object})
{
  required: true,
  ignore_nil: false,
  ignore_blank: false,
  ignore: false,
  composite: false,
  transient: false,
  unwrapped: false,
  prefix: nil,
  access: %i[read write],
  force_type: !Castkit.configuration.enforce_typing
}.freeze

Instance Method Summary collapse

Instance Method Details

#alias_pathsArray<Array<Symbol>>

Returns all alias key paths as arrays of symbols.

Returns:

  • (Array<Array<Symbol>>)


64
65
66
# File 'lib/castkit/ext/attribute/options.rb', line 64

def alias_paths
  options[:aliases].map { |a| a.to_s.split(".").map(&:to_sym) }
end

#composite?Boolean

Whether the attribute is considered composite.

Returns:

  • (Boolean)


113
114
115
# File 'lib/castkit/ext/attribute/options.rb', line 113

def composite?
  options[:composite]
end

#dataobject?Boolean

Whether the attribute is a nested Castkit::DataObject.

Returns:

  • (Boolean)


99
100
101
# File 'lib/castkit/ext/attribute/options.rb', line 99

def dataobject?
  Castkit.dataobject?(type)
end

#dataobject_collection?Boolean

Whether the attribute is a collection of Castkit::DataObjects.

Returns:

  • (Boolean)


106
107
108
# File 'lib/castkit/ext/attribute/options.rb', line 106

def dataobject_collection?
  type == :array && Castkit.dataobject?(options[:of])
end

#defaultObject

Returns the default value for the attribute.

If the default is callable, it is invoked.

Returns:

  • (Object)


34
35
36
37
# File 'lib/castkit/ext/attribute/options.rb', line 34

def default
  val = @default
  val.respond_to?(:call) ? val.call : val
end

#ignore_blank?Boolean

Whether to ignore blank values (‘[]`, `{}`, empty strings) during serialization.

Returns:

  • (Boolean)


92
93
94
# File 'lib/castkit/ext/attribute/options.rb', line 92

def ignore_blank?
  options[:ignore_blank]
end

#ignore_nil?Boolean

Whether to ignore ‘nil` values during serialization.

Returns:

  • (Boolean)


85
86
87
# File 'lib/castkit/ext/attribute/options.rb', line 85

def ignore_nil?
  options[:ignore_nil]
end

#keySymbol, String

Returns the serialization/deserialization key.

Falls back to the field name if ‘:key` is not specified.

Returns:

  • (Symbol, String)


44
45
46
# File 'lib/castkit/ext/attribute/options.rb', line 44

def key
  options[:key] || field
end

#key_path(with_aliases: false) ⇒ Array<Array<Symbol>>

Returns the key path for accessing nested keys.

Optionally includes alias key paths if ‘with_aliases` is true.

Parameters:

  • with_aliases (Boolean) (defaults to: false)

Returns:

  • (Array<Array<Symbol>>)

    nested key paths



54
55
56
57
58
59
# File 'lib/castkit/ext/attribute/options.rb', line 54

def key_path(with_aliases: false)
  path = key.to_s.split(".").map(&:to_sym) || []
  return path unless with_aliases

  [path] + alias_paths
end

#optional?Boolean

Whether the attribute is optional.

Returns:

  • (Boolean)


78
79
80
# File 'lib/castkit/ext/attribute/options.rb', line 78

def optional?
  !required?
end

#prefixString?

Returns the prefix used for unwrapped attributes.

Returns:

  • (String, nil)


136
137
138
# File 'lib/castkit/ext/attribute/options.rb', line 136

def prefix
  options[:prefix]
end

#required?Boolean

Whether the attribute is required for object construction.

Returns:

  • (Boolean)


71
72
73
# File 'lib/castkit/ext/attribute/options.rb', line 71

def required?
  options[:required]
end

#transient?Boolean

Whether the attribute is considered transient (not exposed in serialized output).

Returns:

  • (Boolean)


120
121
122
# File 'lib/castkit/ext/attribute/options.rb', line 120

def transient?
  options[:transient]
end

#unwrapped?Boolean

Whether the attribute is unwrapped into the parent object.

Only applies to Castkit::DataObject types.

Returns:

  • (Boolean)


129
130
131
# File 'lib/castkit/ext/attribute/options.rb', line 129

def unwrapped?
  dataobject? && options[:unwrapped]
end