Class: Domainic::Command::Context::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/domainic/command/context/attribute.rb

Overview

Represents an attribute within a command context. This class manages the lifecycle of an attribute, including its validation, default values, and metadata such as descriptions

The Attribute class supports a variety of configuration options, such as marking an attribute as required, defining static or dynamic default values, and specifying custom validators. These features ensure that attributes conform to expected rules and provide useful metadata for documentation or runtime behavior

Author:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAttribute

Create a new attribute instance

name, type_validator_or_description = nil, description_or_type_validator = nil, **options )

Parameters:

  • name (String, Symbol)

    The #name of the attribute

  • type_validator_or_description (Class, Module, Object, Proc, String, nil)

    A type validator or the #description of the attribute

  • description_or_type_validator (Class, Module, Object, Proc, String, nil)

    The #description or a type_validator of the attribute

  • options (Hash)

    Configuration options for the attribute

Since:

  • 0.1.0



70
71
72
73
74
75
76
77
78
# File 'lib/domainic/command/context/attribute.rb', line 70

def initialize(name, *type_validator_and_description, **options)
  symbolized_options = options.transform_keys(&:to_sym)

  @name = name.to_sym
  @required = symbolized_options[:required] == true

  initialize_default(symbolized_options)
  initialize_description_and_type_validator(type_validator_and_description, symbolized_options)
end

Instance Attribute Details

#descriptionString? (readonly)

The textual description of the attribute, providing metadata about its purpose or usage

Returns:

  • (String, nil)

    A description of the attribute

Since:

  • 0.1.0



32
33
34
# File 'lib/domainic/command/context/attribute.rb', line 32

def description
  @description
end

#nameSymbol (readonly)

The name of the attribute, uniquely identifying it within a command context

Returns:

  • (Symbol)

    The name of the attribute

Since:

  • 0.1.0



37
38
39
# File 'lib/domainic/command/context/attribute.rb', line 37

def name
  @name
end

Instance Method Details

#defaultObject?

Retrieves the default value of the attribute. If a default generator is specified, it evaluates the generator and returns the result

Returns:

  • (Object, nil)

    The default value or the result of the generator

Since:

  • 0.1.0



85
86
87
88
89
# File 'lib/domainic/command/context/attribute.rb', line 85

def default
  return unless default?

  @default.is_a?(Proc) ? @default.call : @default
end

#default?Boolean

Determines whether the attribute has a default value defined

Returns:

  • (Boolean)

    true if a default is set; otherwise, false

Since:

  • 0.1.0



95
96
97
# File 'lib/domainic/command/context/attribute.rb', line 95

def default?
  @default != UNDEFINED_DEFAULT
end

#required?Boolean

Determines whether the attribute is marked as required

Returns:

  • (Boolean)

    true if the attribute is required; otherwise, false

Since:

  • 0.1.0



103
104
105
# File 'lib/domainic/command/context/attribute.rb', line 103

def required?
  @required
end

#valid?(value) ⇒ Boolean

Validates the given value against the attribute's type validator

Parameters:

  • value (Object)

    The value to validate

Returns:

  • (Boolean)

    true if the value is valid; otherwise, false

Since:

  • 0.1.0



113
114
115
116
117
118
119
120
121
# File 'lib/domainic/command/context/attribute.rb', line 113

def valid?(value)
  return false if value.nil? && required?
  return true if @type_validator.nil?

  validator = @type_validator
  return validator.call(value) if validator.is_a?(Proc)

  validator === value || value.is_a?(validator) # rubocop:disable Style/CaseEquality
end