Class: SpecForge::Attribute::ResolvableArray

Inherits:
Array
  • Object
show all
Includes:
Resolvable
Defined in:
lib/spec_forge/attribute/resolvable_array.rb

Overview

Represents an array that may contain attributes that need resolution

This class extends Array and provides methods to recursively resolve any attribute objects contained within it. It allows arrays to contain dynamic content like variables and faker values.

Examples:

In code

array = [1, Attribute::Variable.new("variables.user_id"), 3]
resolvable = Attribute::ResolvableArray.new(array)
resolvable.resolved # => [1, 42, 3]  # assuming user_id resolves to 42

Instance Method Summary collapse

Methods included from Resolvable

#resolve_as_matcher_proc, #resolve_proc, #resolved_proc

Methods inherited from Array

#to_merged_h

Constructor Details

#initialize(array = []) ⇒ ResolvableArray

Creates a new ResolvableArray from the given array

Parameters:

  • array (Array) (defaults to: [])

    The array to wrap



25
26
27
# File 'lib/spec_forge/attribute/resolvable_array.rb', line 25

def initialize(array = [])
  super
end

Instance Method Details

#resolveArray

Freshly resolves all items in the array. Unlike #resolved, this doesn't use cached values, ensuring fresh resolution.

Examples:

array_attr = Attribute::ResolvableArray.new([Attribute::Faker.new("faker.name.name")])
array_attr.resolve # => ["John Smith"] (fresh value each time)

Returns:

  • (Array)

    A new array with all items freshly resolved



62
63
64
# File 'lib/spec_forge/attribute/resolvable_array.rb', line 62

def resolve
  map(&resolve_proc)
end

#resolve_as_matcherRSpec::Matchers::BuiltIn::BaseMatcher

Converts all items in the array to RSpec matchers. First converts each array element to a matcher using resolve_as_matcher_proc, then wraps the entire result in a matcher suitable for array comparison.

This ensures all elements in the array are proper matchers, which is essential for compound matchers and proper failure messages.

Examples:

array = Attribute::ResolvableArray.new(["test", /pattern/, 42])
array.resolve_as_matcher # => contain_exactly(eq("test"), match(/pattern/), eq(42))

Returns:

  • (RSpec::Matchers::BuiltIn::BaseMatcher)

    A matcher for this array



80
81
82
83
# File 'lib/spec_forge/attribute/resolvable_array.rb', line 80

def resolve_as_matcher
  result = map(&resolve_as_matcher_proc)
  Attribute::Literal.new(result).resolve_as_matcher
end

#resolvedArray

Returns a new array with all items fully resolved to their final values. Uses the cached version of each item if available.

Examples:

array_attr = Attribute::ResolvableArray.new([Attribute::Faker.new("faker.name.name")])
array_attr.resolved # => ["Jane Doe"] (with result cached)

Returns:

  • (Array)

    A new array with all items fully resolved to their final values



48
49
50
# File 'lib/spec_forge/attribute/resolvable_array.rb', line 48

def resolved
  map(&resolved_proc)
end

#valueArray

Returns the underlying array

Returns:

  • (Array)

    The array itself



34
35
36
# File 'lib/spec_forge/attribute/resolvable_array.rb', line 34

def value
  self
end