Module: Castkit::AttributeExtensions::Options

Included in:
Castkit::Attribute
Defined in:
lib/castkit/attribute_extensions/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,
  unwrapped: false,
  prefix: nil,
  access: %i[read write]
}.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>>)


61
62
63
# File 'lib/castkit/attribute_extensions/options.rb', line 61

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

#composite?Boolean

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

Returns:

  • (Boolean)


110
111
112
# File 'lib/castkit/attribute_extensions/options.rb', line 110

def composite?
  options[:composite]
end

#dataobject?Boolean

Whether the attribute is a nested Castkit::DataObject.

Returns:

  • (Boolean)


96
97
98
# File 'lib/castkit/attribute_extensions/options.rb', line 96

def dataobject?
  Castkit.dataobject?(type)
end

#dataobject_collection?Boolean

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

Returns:

  • (Boolean)


103
104
105
# File 'lib/castkit/attribute_extensions/options.rb', line 103

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)


31
32
33
34
# File 'lib/castkit/attribute_extensions/options.rb', line 31

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)


89
90
91
# File 'lib/castkit/attribute_extensions/options.rb', line 89

def ignore_blank?
  options[:ignore_blank]
end

#ignore_nil?Boolean

Whether to ignore ‘nil` values during serialization.

Returns:

  • (Boolean)


82
83
84
# File 'lib/castkit/attribute_extensions/options.rb', line 82

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)


41
42
43
# File 'lib/castkit/attribute_extensions/options.rb', line 41

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



51
52
53
54
55
56
# File 'lib/castkit/attribute_extensions/options.rb', line 51

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)


75
76
77
# File 'lib/castkit/attribute_extensions/options.rb', line 75

def optional?
  !required?
end

#prefixString?

Returns the prefix used for unwrapped attributes.

Returns:

  • (String, nil)


126
127
128
# File 'lib/castkit/attribute_extensions/options.rb', line 126

def prefix
  options[:prefix]
end

#required?Boolean

Whether the attribute is required for object construction.

Returns:

  • (Boolean)


68
69
70
# File 'lib/castkit/attribute_extensions/options.rb', line 68

def required?
  options[:required]
end

#unwrapped?Boolean

Whether the attribute is unwrapped into the parent object.

Only applies to Castkit::DataObject types.

Returns:

  • (Boolean)


119
120
121
# File 'lib/castkit/attribute_extensions/options.rb', line 119

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