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.
{ 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
-
#alias_paths ⇒ Array<Array<Symbol>>
Returns all alias key paths as arrays of symbols.
-
#composite? ⇒ Boolean
Whether the attribute is considered composite (not exposed in serialized output).
-
#dataobject? ⇒ Boolean
Whether the attribute is a nested Castkit::DataObject.
-
#dataobject_collection? ⇒ Boolean
Whether the attribute is a collection of Castkit::DataObjects.
-
#default ⇒ Object
Returns the default value for the attribute.
-
#ignore_blank? ⇒ Boolean
Whether to ignore blank values (‘[]`, `{}`, empty strings) during serialization.
-
#ignore_nil? ⇒ Boolean
Whether to ignore ‘nil` values during serialization.
-
#key ⇒ Symbol, String
Returns the serialization/deserialization key.
-
#key_path(with_aliases: false) ⇒ Array<Array<Symbol>>
Returns the key path for accessing nested keys.
-
#optional? ⇒ Boolean
Whether the attribute is optional.
-
#prefix ⇒ String?
Returns the prefix used for unwrapped attributes.
-
#required? ⇒ Boolean
Whether the attribute is required for object construction.
-
#unwrapped? ⇒ Boolean
Whether the attribute is unwrapped into the parent object.
Instance Method Details
#alias_paths ⇒ Array<Array<Symbol>>
Returns all alias key paths as arrays of symbols.
61 62 63 |
# File 'lib/castkit/attribute_extensions/options.rb', line 61 def alias_paths [:aliases].map { |a| a.to_s.split(".").map(&:to_sym) } end |
#composite? ⇒ Boolean
Whether the attribute is considered composite (not exposed in serialized output).
110 111 112 |
# File 'lib/castkit/attribute_extensions/options.rb', line 110 def composite? [:composite] end |
#dataobject? ⇒ Boolean
Whether the attribute is a nested Castkit::DataObject.
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.
103 104 105 |
# File 'lib/castkit/attribute_extensions/options.rb', line 103 def dataobject_collection? type == :array && Castkit.dataobject?([:of]) end |
#default ⇒ Object
Returns the default value for the attribute.
If the default is callable, it is invoked.
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.
89 90 91 |
# File 'lib/castkit/attribute_extensions/options.rb', line 89 def ignore_blank? [:ignore_blank] end |
#ignore_nil? ⇒ Boolean
Whether to ignore ‘nil` values during serialization.
82 83 84 |
# File 'lib/castkit/attribute_extensions/options.rb', line 82 def ignore_nil? [:ignore_nil] end |
#key ⇒ Symbol, String
Returns the serialization/deserialization key.
Falls back to the field name if ‘:key` is not specified.
41 42 43 |
# File 'lib/castkit/attribute_extensions/options.rb', line 41 def key [: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.
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.
75 76 77 |
# File 'lib/castkit/attribute_extensions/options.rb', line 75 def optional? !required? end |
#prefix ⇒ String?
Returns the prefix used for unwrapped attributes.
126 127 128 |
# File 'lib/castkit/attribute_extensions/options.rb', line 126 def prefix [:prefix] end |
#required? ⇒ Boolean
Whether the attribute is required for object construction.
68 69 70 |
# File 'lib/castkit/attribute_extensions/options.rb', line 68 def required? [:required] end |
#unwrapped? ⇒ Boolean
Whether the attribute is unwrapped into the parent object.
Only applies to Castkit::DataObject types.
119 120 121 |
# File 'lib/castkit/attribute_extensions/options.rb', line 119 def unwrapped? dataobject? && [:unwrapped] end |