Class: SpecForge::Attribute::ResolvableHash

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

Overview

Represents a hash that may contain attributes that need resolution

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

Examples:

In code

hash = {name: Attribute::Faker.new("faker.name.name"), id: 123}
resolvable = Attribute::ResolvableHash.new(hash)
resolvable.resolved # => {name: "John Smith", id: 123}

Instance Method Summary collapse

Methods included from Resolvable

#resolve_as_matcher_proc, #resolve_proc, #resolved_proc

Constructor Details

#initialize(hash = {}) ⇒ ResolvableHash

Creates a new ResolvableHash from the given hash

Parameters:

  • hash (Hash) (defaults to: {})

    The hash to wrap



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

def initialize(hash = {})
  super()
  merge!(hash)
end

Instance Method Details

#resolveHash

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

Examples:

hash_attr = Attribute::ResolvableHash.new({name: Attribute::Faker.new("faker.name.name")})
hash_attr.resolve # => {name: "John Smith"} (fresh value each time)

Returns:

  • (Hash)

    A new hash with all values freshly resolved



63
64
65
# File 'lib/spec_forge/attribute/resolvable_hash.rb', line 63

def resolve
  transform_values(&resolve_proc)
end

#resolve_as_matcherRSpec::Matchers::BuiltIn::BaseMatcher

Converts all values in the hash to RSpec matchers. Transforms each hash value to a matcher using resolve_as_matcher_proc, then wraps the entire result in a matcher suitable for hash comparison.

This ensures proper nesting of matchers in hash structures, which is vital for readable failure messages in complex expectations.

Examples:

hash = Attribute::ResolvableHash.new({name: "Test", age: 42})
hash.resolve_as_matcher # => include("name" => eq("Test"), "age" => eq(42))

Returns:

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

    A matcher for this hash



81
82
83
84
# File 'lib/spec_forge/attribute/resolvable_hash.rb', line 81

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

#resolvedHash

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

Examples:

hash_attr = Attribute::ResolvableHash.new({name: Attribute::Faker.new("faker.name.name")})
hash_attr.resolved # => {name: "Jane Doe"} (with result cached)

Returns:

  • (Hash)

    A new hash with all values fully resolved to their final values



49
50
51
# File 'lib/spec_forge/attribute/resolvable_hash.rb', line 49

def resolved
  transform_values(&resolved_proc)
end

#valueHash

Returns the underlying hash

Returns:

  • (Hash)

    The hash itself



35
36
37
# File 'lib/spec_forge/attribute/resolvable_hash.rb', line 35

def value
  self
end