Class: SpecForge::Attribute::Parameterized

Inherits:
Attribute
  • Object
show all
Defined in:
lib/spec_forge/attribute/parameterized.rb

Overview

Base class for attributes that support positional and keyword arguments

This class provides the foundation for attributes that need to accept arguments, such as Faker, Matcher, and Factory. It handles both positional (array-style) and keyword (hash-style) arguments.

Examples:

With keyword arguments in YAML

example:
  keyword:
    arg1: value1
    arg2: value2

With positional arguments in YAML

example:
  keyword:
  - arg1
  - arg2

Direct Known Subclasses

Factory, Faker, Generate, Matcher, Transform

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParameterized

Creates a new parameterized attribute with the specified arguments

Parameters:

  • input (String, Symbol)

    The key that contains these arguments

  • options (Hash)

    Options including positional and keyword arguments



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/spec_forge/attribute/parameterized.rb', line 70

def initialize(...)
  super

  @input = @input.to_s.downcase

  @arguments = {
    positional: @options[:positional] || [],
    keyword: @options[:keyword] || {}
  }

  @options.clear # No need to store a duplicate
end

Instance Attribute Details

#argumentsHash{Symbol => Object} (readonly)

A hash containing both positional and keyword arguments for this attribute The hash has two keys: :positional (Array) and :keyword (Hash)

Returns:

  • (Hash{Symbol => Object})

    The arguments hash with structure: { positional: Array - Contains positional arguments in order keyword: Hash - Contains keyword arguments as key-value pairs }



60
61
62
# File 'lib/spec_forge/attribute/parameterized.rb', line 60

def arguments
  @arguments
end

Class Method Details

.from_hash(hash, **options) ⇒ Parameterized

Creates a new attribute instance from a hash representation

Parameters:

  • hash (Hash)

    A hash containing the attribute name and arguments

  • options (Hash)

    Additional options to pass to the attribute (e.g., context)

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spec_forge/attribute/parameterized.rb', line 33

def self.from_hash(hash, **options)
   = hash.first

  input = .first
  arguments = .second

  case arguments
  when Array
    new(input, positional: arguments, **options)
  when Hash
    new(input, keyword: arguments, **options)
  else
    # Single value
    new(input, positional: [arguments], **options)
  end
end