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.
{ 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
-
#alias_paths ⇒ Array<Array<Symbol>>
Returns all alias key paths as arrays of symbols.
-
#composite? ⇒ Boolean
Whether the attribute is considered composite.
-
#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.
-
#transient? ⇒ Boolean
Whether the attribute is considered transient (not exposed in serialized output).
-
#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.
64 65 66 |
# File 'lib/castkit/ext/attribute/options.rb', line 64 def alias_paths [:aliases].map { |a| a.to_s.split(".").map(&:to_sym) } end |
#composite? ⇒ Boolean
Whether the attribute is considered composite.
113 114 115 |
# File 'lib/castkit/ext/attribute/options.rb', line 113 def composite? [:composite] end |
#dataobject? ⇒ Boolean
Whether the attribute is a nested Castkit::DataObject.
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.
106 107 108 |
# File 'lib/castkit/ext/attribute/options.rb', line 106 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.
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.
92 93 94 |
# File 'lib/castkit/ext/attribute/options.rb', line 92 def ignore_blank? [:ignore_blank] end |
#ignore_nil? ⇒ Boolean
Whether to ignore ‘nil` values during serialization.
85 86 87 |
# File 'lib/castkit/ext/attribute/options.rb', line 85 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.
44 45 46 |
# File 'lib/castkit/ext/attribute/options.rb', line 44 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.
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.
78 79 80 |
# File 'lib/castkit/ext/attribute/options.rb', line 78 def optional? !required? end |
#prefix ⇒ String?
Returns the prefix used for unwrapped attributes.
136 137 138 |
# File 'lib/castkit/ext/attribute/options.rb', line 136 def prefix [:prefix] end |
#required? ⇒ Boolean
Whether the attribute is required for object construction.
71 72 73 |
# File 'lib/castkit/ext/attribute/options.rb', line 71 def required? [:required] end |
#transient? ⇒ Boolean
Whether the attribute is considered transient (not exposed in serialized output).
120 121 122 |
# File 'lib/castkit/ext/attribute/options.rb', line 120 def transient? [:transient] end |
#unwrapped? ⇒ Boolean
Whether the attribute is unwrapped into the parent object.
Only applies to Castkit::DataObject types.
129 130 131 |
# File 'lib/castkit/ext/attribute/options.rb', line 129 def unwrapped? dataobject? && [:unwrapped] end |