Module: Castkit::Attributes::Options

Included in:
Definition
Defined in:
lib/castkit/attributes/options.rb

Overview

Provides a DSL for configuring attribute options within an attribute definition.

This module is designed to be extended by class-level definition objects such as ‘Castkit::Attributes::Definition`, and is used to build reusable sets of options for attributes declared within `Castkit::DataObject` classes.

Examples:

class OptionalString < Castkit::Attributes::Definition
  type :string
  required false
  ignore_blank true
end

Constant Summary collapse

ACCESS_MODES =

Valid access modes for an attribute.

Returns:

  • (Array<Symbol>)
%i[read write].freeze
DEFAULTS =

Default configuration for attribute options.

Returns:

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

Instance Method Summary collapse

Instance Method Details

#access(value = nil) ⇒ void

This method returns an undefined value.

Sets access modes for the attribute.

Parameters:

  • value (Array<Symbol>, Symbol) (defaults to: nil)

    valid values: ‘:read`, `:write`, or both



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

def access(value = nil)
  value = validate_access_modes!(value)
  set_option(:access, value)
end

#composite(value = nil) ⇒ void

This method returns an undefined value.

Marks the attribute as a composite (e.g., nested ‘DataObject`).

Parameters:

  • value (Boolean) (defaults to: nil)


143
144
145
# File 'lib/castkit/attributes/options.rb', line 143

def composite(value = nil)
  set_option(:composite, value || true)
end

#default(value = nil) ⇒ void

This method returns an undefined value.

Sets the default value or proc for the attribute.

Parameters:

  • value (Object, Proc) (defaults to: nil)

    the default value or lambda



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

def default(value = nil)
  set_option(:default, value)
end

#force_type(value = nil) ⇒ void

This method returns an undefined value.

Enables or disables forced typecasting, or sets a custom flag.

Parameters:

  • value (Boolean, nil) (defaults to: nil)

    the forced type flag



69
70
71
# File 'lib/castkit/attributes/options.rb', line 69

def force_type(value = nil)
  set_option(:force_type, value || !Castkit.configuration.enforce_typing)
end

#format(value) ⇒ void

This method returns an undefined value.

Sets a format constraint (e.g., regex validation).

Parameters:

  • value (Regexp)


159
160
161
# File 'lib/castkit/attributes/options.rb', line 159

def format(value)
  set_option(:format, value)
end

#ignore(value = nil) ⇒ void

This method returns an undefined value.

Marks the attribute to be ignored entirely.

Parameters:

  • value (Boolean) (defaults to: nil)


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

def ignore(value = nil)
  set_option(:ignore, value || true)
end

#ignore_blank(value = nil) ⇒ void

This method returns an undefined value.

Ignores blank values (‘“”`, `[]`, `{}`) during serialization.

Parameters:

  • value (Boolean) (defaults to: nil)


101
102
103
# File 'lib/castkit/attributes/options.rb', line 101

def ignore_blank(value = nil)
  set_option(:ignore_blank, value || true)
end

#ignore_nil(value = nil) ⇒ void

This method returns an undefined value.

Ignores ‘nil` values during serialization or persistence.

Parameters:

  • value (Boolean) (defaults to: nil)


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

def ignore_nil(value = nil)
  set_option(:ignore_nil, value || true)
end

#of(value) ⇒ void

This method returns an undefined value.

Sets the element type for array attributes.

Parameters:

  • value (Symbol, Class)

    the type of elements in the array



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

def of(value)
  return unless @type == :array

  set_option(:of, value)
end

#prefix(value = nil) ⇒ void

This method returns an undefined value.

Adds a prefix for unwrapped attribute keys.

Parameters:

  • value (String, Symbol, nil) (defaults to: nil)


109
110
111
# File 'lib/castkit/attributes/options.rb', line 109

def prefix(value = nil)
  set_option(:prefix, value)
end

#readonly(value = nil) ⇒ void

This method returns an undefined value.

Shortcut to make the attribute readonly (‘access: [:read]`).

Parameters:

  • value (Boolean) (defaults to: nil)


134
135
136
137
# File 'lib/castkit/attributes/options.rb', line 134

def readonly(value = nil)
  value = value || true ? [:read] : ACCESS_MODES
  set_option(:access, value)
end

#required(value = nil) ⇒ void

This method returns an undefined value.

Marks the attribute as required or optional.

Parameters:

  • value (Boolean) (defaults to: nil)


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

def required(value = nil)
  set_option(:required, value || true)
end

#transient(value = nil) ⇒ void

This method returns an undefined value.

Marks the attribute as transient (not included in persistence or serialization).

Parameters:

  • value (Boolean) (defaults to: nil)


151
152
153
# File 'lib/castkit/attributes/options.rb', line 151

def transient(value = nil)
  set_option(:transient, value || true)
end

#type(value = nil) ⇒ Symbol

Sets or retrieves the attribute type.

Parameters:

  • value (Symbol, nil) (defaults to: nil)

    The type to assign (e.g., :string), or nil to fetch.

Returns:

  • (Symbol)


43
44
45
# File 'lib/castkit/attributes/options.rb', line 43

def type(value = nil)
  value.nil? ? definition[:type] : (definition[:type] = value.to_sym)
end

#unwrapped(value = nil) ⇒ void

This method returns an undefined value.

Marks the attribute as unwrapped (inline merging of nested fields).

Parameters:

  • value (Boolean) (defaults to: nil)


117
118
119
# File 'lib/castkit/attributes/options.rb', line 117

def unwrapped(value = nil)
  set_option(:unwrapped, value || true)
end

#validator(value) ⇒ void

This method returns an undefined value.

Attaches a custom validator callable for this attribute.

Parameters:

  • value (Proc, #call)


167
168
169
# File 'lib/castkit/attributes/options.rb', line 167

def validator(value)
  set_option(:validator, value)
end