Module: Domainic::Attributer::ClassMethods

Defined in:
lib/domainic/attributer/class_methods.rb

Overview

A module providing class-level methods for attribute definition.

This module extends classes that include Domainic::Attributer with methods for defining and managing attributes. It supports two types of attributes:

  1. Arguments - Positional parameters that must be provided in a specific order

  2. Options - Named parameters that can be provided in any order

Examples:

Defining arguments and options

class Person
  include Domainic::Attributer

  argument :name, ->(value) { value.is_a?(String) }
  argument :age do |value|
    value.is_a?(Integer) && value >= 0
  end

  option :email, ->(value) { value.is_a?(String) }, default: nil
  option :role do |value|
    %w[admin user guest].include?(value)
  end
end

Author:

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#argument(attribute_name, type_validator = Undefined, **options) {|DSL::AttributeBuilder| ... } ⇒ void

This method returns an undefined value.

Define a positional argument attribute.

Arguments are required by default and must be provided in the order they are defined. They can be type-validated and configured with additional options like defaults and visibility.

Parameters:

  • attribute_name (String, Symbol)

    the name of the attribute

  • type_validator (Proc, Object, nil) (defaults to: Undefined)

    optional validation handler for type checking

  • options (Hash)

    additional configuration options

Options Hash (**options):

  • :callbacks (Array<Proc>, Proc)

    handlers for attribute change events (priority over :callback, :on_change)

  • :callback (Array<Proc>, Proc)

    alias for :callbacks

  • :coerce (Array<Proc, Symbol>, Proc, Symbol)

    handlers for value coercion (priority over :coercers, :coerce_with)

  • :coercers (Array<Proc, Symbol>, Proc, Symbol)

    alias for :coerce

  • :coerce_with (Array<Proc, Symbol>, Proc, Symbol)

    alias for :coerce

  • :default (Object)

    the default value (priority over :default_generator, :default_value)

  • :default_generator (Object)

    alias for :default

  • :default_value (Object)

    alias for :default

  • :desc (String)

    short description (overridden by :description)

  • :description (String)

    description text

  • :non_nil (Boolean)

    require non-nil values (priority over :non_null, :non_nullable, :not_nil, :not_nilable, :not_null, :not_nullable)

  • :non_null (Boolean)

    alias for :non_nil

  • :non_nullable (Boolean)

    alias for :non_nil

  • :not_nil (Boolean)

    alias for :non_nil

  • :not_nilable (Boolean)

    alias for :non_nil

  • :not_null (Boolean)

    alias for :non_nil

  • :not_nullable (Boolean)

    alias for :non_nil

  • :null (Boolean)

    inverse of :non_nil

  • :on_change (Array<Proc>, Proc)

    alias for :callbacks

  • :optional (Boolean)

    whether attribute is optional (overridden by :required)

  • :position (Integer)

    specify order position

  • :read (Symbol)

    read visibility (:public, :protected, :private) (priority over :read_access, :reader)

  • :read_access (Symbol)

    alias for :read

  • :reader (Symbol)

    alias for :read

  • :required (Boolean)

    whether attribute is required

  • :validate (Array<Object>, Object)

    validators for the attribute (priority over :validate_with, :validators)

  • :validate_with (Array<Object>, Object)

    alias for :validate

  • :validators (Array<Object>, Object)

    alias for :validate

  • :write_access (Symbol)

    write visibility (:public, :protected, :private) (priority over :writer)

  • :writer (Symbol)

    alias for :write_access

Yields:

Since:

  • 0.1.0



119
120
121
122
123
124
125
126
127
128
# File 'lib/domainic/attributer/class_methods.rb', line 119

def argument(attribute_name, type_validator = Undefined, **options, &)
  position = __attributes__.count { |_, attribute| attribute.signature.argument? }

  attribute = DSL::AttributeBuilder.new(
    self, attribute_name, :argument, type_validator, **options.merge(position:), &
  ).build!

  __attributes__.add(attribute)
  DSL::MethodInjector.inject!(self, attribute)
end

#option(attribute_name, type_validator = Undefined, **options, &block) ⇒ void

This method returns an undefined value.

Define a named option attribute.

Options are optional by default and can be provided in any order. They can be type-validated and configured with additional options like defaults and visibility.

Parameters:

  • attribute_name (String, Symbol)

    the name of the attribute

  • type_validator (Proc, Object, nil) (defaults to: Undefined)

    optional validation handler for type checking

  • options (Hash)

    additional configuration options

Options Hash (**options):

  • :callbacks (Array<Proc>, Proc)

    handlers for attribute change events (priority over :callback, :on_change)

  • :callback (Array<Proc>, Proc)

    alias for :callbacks

  • :coerce (Array<Proc, Symbol>, Proc, Symbol)

    handlers for value coercion (priority over :coercers, :coerce_with)

  • :coercers (Array<Proc, Symbol>, Proc, Symbol)

    alias for :coerce

  • :coerce_with (Array<Proc, Symbol>, Proc, Symbol)

    alias for :coerce

  • :default (Object)

    the default value (priority over :default_generator, :default_value)

  • :default_generator (Object)

    alias for :default

  • :default_value (Object)

    alias for :default

  • :desc (String)

    short description (overridden by :description)

  • :description (String)

    description text

  • :non_nil (Boolean)

    require non-nil values (priority over :non_null, :non_nullable, :not_nil, :not_nilable, :not_null, :not_nullable)

  • :non_null (Boolean)

    alias for :non_nil

  • :non_nullable (Boolean)

    alias for :non_nil

  • :not_nil (Boolean)

    alias for :non_nil

  • :not_nilable (Boolean)

    alias for :non_nil

  • :not_null (Boolean)

    alias for :non_nil

  • :not_nullable (Boolean)

    alias for :non_nil

  • :null (Boolean)

    inverse of :non_nil

  • :on_change (Array<Proc>, Proc)

    alias for :callbacks

  • :optional (Boolean)

    whether attribute is optional (overridden by :required)

  • :position (Integer)

    specify order position

  • :read (Symbol)

    read visibility (:public, :protected, :private) (priority over :read_access, :reader)

  • :read_access (Symbol)

    alias for :read

  • :reader (Symbol)

    alias for :read

  • :required (Boolean)

    whether attribute is required

  • :validate (Array<Object>, Object)

    validators for the attribute (priority over :validate_with, :validators)

  • :validate_with (Array<Object>, Object)

    alias for :validate

  • :validators (Array<Object>, Object)

    alias for :validate

  • :write_access (Symbol)

    write visibility (:public, :protected, :private) (priority over :writer)

  • :writer (Symbol)

    alias for :write_access

Yields:

Since:

  • 0.1.0



216
217
218
219
220
221
# File 'lib/domainic/attributer/class_methods.rb', line 216

def option(attribute_name, ...)
  attribute = DSL::AttributeBuilder.new(self, attribute_name, :option, ...).build! # steep:ignore

  __attributes__.add(attribute)
  DSL::MethodInjector.inject!(self, attribute)
end