Class: CMDx::AttributeValue

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cmdx/attribute_value.rb

Overview

Manages the value lifecycle for a single attribute within a task. Handles value sourcing, derivation, coercion, and validation through a coordinated pipeline that ensures data integrity and type safety.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ AttributeValue

Creates a new attribute value manager for the given attribute.

Examples:

attr = Attribute.new(:user_id, required: true)
attr_value = AttributeValue.new(attr)

Parameters:

  • attribute (Attribute)

    The attribute to manage values for



33
34
35
# File 'lib/cmdx/attribute_value.rb', line 33

def initialize(attribute)
  @attribute = attribute
end

Instance Attribute Details

#attributeAttribute (readonly)

Returns the attribute managed by this value handler.

Examples:

attr_value.attribute.name # => :user_id

Returns:



19
20
21
# File 'lib/cmdx/attribute_value.rb', line 19

def attribute
  @attribute
end

Instance Method Details

#generateObject?

Generates the attribute value through the complete pipeline: sourcing, derivation, coercion, and storage.

Examples:

attr_value.generate # => 42

Returns:

  • (Object, nil)

    The generated value or nil if generation failed



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cmdx/attribute_value.rb', line 58

def generate
  return value if attributes.key?(method_name)

  sourced_value = source_value
  return if errors.for?(method_name)

  derived_value = derive_value(sourced_value)
  return if errors.for?(method_name)

  coerced_value = coerce_value(derived_value)
  transformed_value = transform_value(coerced_value)
  return if errors.for?(method_name)

  attributes[method_name] = transformed_value
end

#validateObject

Validates the current attribute value against configured validators.

Examples:

attr_value.validate
# Validates value against :presence, :format, etc.

Raises:



83
84
85
86
87
88
89
90
91
92
# File 'lib/cmdx/attribute_value.rb', line 83

def validate
  registry = task.class.settings[:validators]

  options.slice(*registry.keys).each do |type, opts|
    registry.validate(type, task, value, opts)
  rescue ValidationError => e
    errors.add(method_name, e.message)
    nil
  end
end

#valueObject?

Retrieves the current value for this attribute from the task’s attributes.

Examples:

attr_value.value # => "john_doe"

Returns:

  • (Object, nil)

    The current attribute value or nil if not set



45
46
47
# File 'lib/cmdx/attribute_value.rb', line 45

def value
  attributes[method_name]
end