Class: Servactory::ToolKit::DynamicOptions::Must::WorkOption

Inherits:
Object
  • Object
show all
Defined in:
lib/servactory/tool_kit/dynamic_options/must.rb

Overview

Value object representing a parsed validation option.

## Purpose

WorkOption normalizes the various ways an option can be specified (simple value, hash with ‘:is` key, hash with custom message) into a consistent interface for validators.

## Attributes

  • ‘name` - The option name symbol (e.g., `:min`, `:max`)

  • ‘value` - The actual validation value (e.g., `10` for `min: 10`)

  • ‘message` - Custom error message (String or Proc), or nil

  • ‘properties` - Additional hash properties passed with the option

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data, body_key:, body_fallback:) ⇒ WorkOption

Creates a new WorkOption by parsing option data.

Parameters:

  • name (Symbol)

    The option name

  • data (Object)

    Raw option value (can be scalar, Hash, etc.)

  • body_key (Symbol)

    Key to extract value from Hash (default: :is)

  • body_fallback (Object)

    Default value if data is empty



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/servactory/tool_kit/dynamic_options/must.rb', line 125

def initialize(name, data, body_key:, body_fallback:)
  @name = name

  # Extract the primary value from data.
  @value =
    if data.is_a?(Hash) && data.key?(body_key)
      data.delete(body_key)
    else
      data.presence || body_fallback
    end

  # Extract custom message if provided.
  @message = (data.is_a?(Hash) && data.key?(:message) ? data.delete(:message) : nil)

  # Remaining hash properties become additional configuration.
  @properties = data.is_a?(Hash) ? data : {}
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



114
115
116
# File 'lib/servactory/tool_kit/dynamic_options/must.rb', line 114

def message
  @message
end

#nameObject (readonly)

Returns the value of attribute name.



114
115
116
# File 'lib/servactory/tool_kit/dynamic_options/must.rb', line 114

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



114
115
116
# File 'lib/servactory/tool_kit/dynamic_options/must.rb', line 114

def properties
  @properties
end

#valueObject (readonly)

Returns the value of attribute value.



114
115
116
# File 'lib/servactory/tool_kit/dynamic_options/must.rb', line 114

def value
  @value
end