Class: SpecForge::Attribute::Generate

Inherits:
Parameterized show all
Defined in:
lib/spec_forge/attribute/generate.rb

Overview

Represents an attribute that generates data structures dynamically.

This class provides generation functions like array that can create collections of arbitrary size with evaluated values. It's useful for testing batch endpoints or generating large payloads.

Examples:

Generate an array of static values

generate.array:
  size: 5
  value: "test"

Generate an array with faker values

generate.array:
  size: 100
  value: "{{ faker.string.alphanumeric }}"

Generate an array with sequential indices

generate.array:
  size: 3
  value: "user_{{ index }}"
# Produces: ["user_0", "user_1", "user_2"]

Combine faker with index

generate.array:
  size: 10
  value: "{{ faker.internet.username }}_{{ index }}"

Constant Summary collapse

KEYWORD_REGEX =

Regular expression pattern that matches attribute keywords with this prefix. Used for identifying this attribute type during parsing. Matches case-insensitively (generate., GENERATE., Generate., etc.)

Returns:

  • (Regexp)
/^generate\./i
METHODS =

The available generation methods

Returns:

%w[
  array
].freeze

Instance Attribute Summary collapse

Attributes inherited from Parameterized

#arguments

Instance Method Summary collapse

Methods inherited from Parameterized

from_hash

Constructor Details

#initializeGenerate

Creates a new generate attribute with the specified function and arguments

Raises:

See Also:



66
67
68
69
70
71
72
73
# File 'lib/spec_forge/attribute/generate.rb', line 66

def initialize(...)
  super

  @function = @input.sub(KEYWORD_REGEX, "")
  raise Error::InvalidGenerateFunctionError.new(input, METHODS) unless METHODS.include?(function)

  prepare_arguments
end

Instance Attribute Details

#functionString (readonly)

The generation function name (e.g., "array")

Returns:

  • (String)


57
58
59
# File 'lib/spec_forge/attribute/generate.rb', line 57

def function
  @function
end

Instance Method Details

#valueObject

Returns the result of applying the generation function

Returns:

  • (Object)

    The generated value



80
81
82
83
84
85
# File 'lib/spec_forge/attribute/generate.rb', line 80

def value
  case function
  when "array"
    generate_array
  end
end