Class: SpecForge::Attribute::Faker

Inherits:
Parameterized show all
Includes:
Chainable
Defined in:
lib/spec_forge/attribute/faker.rb

Overview

Represents an attribute that generates fake data using the Faker gem

This class allows SpecForge to integrate with the Faker library to generate realistic test data like names, emails, addresses, etc.

Examples:

Basic usage in YAML

name: faker.name.name
email: faker.internet.email

With method arguments

age:
  faker.number.between:
    from: 18
    to: 65

Handles nested faker classes

character: faker.games.zelda.character

Constant Summary collapse

KEYWORD_REGEX =

Regular expression pattern that matches attribute keywords with this prefix Used for identifying this attribute type during parsing

Returns:

  • (Regexp)
/^faker\./i

Constants included from Chainable

Chainable::NUMBER_REGEX

Instance Attribute Summary collapse

Attributes included from Chainable

#header, #invocation_chain, #keyword

Attributes inherited from Parameterized

#arguments

Instance Method Summary collapse

Methods included from Chainable

#resolved, #value

Methods inherited from Parameterized

from_hash

Constructor Details

#initializeFaker

Creates a new faker attribute with the specified name and arguments

Raises:

See Also:



49
50
51
52
53
54
55
# File 'lib/spec_forge/attribute/faker.rb', line 49

def initialize(...)
  super

  @faker_class, @faker_method = extract_faker_call

  prepare_arguments
end

Instance Attribute Details

#faker_classClass (readonly)

Returns The Faker class.

Returns:

  • (Class)

    The Faker class



36
37
38
# File 'lib/spec_forge/attribute/faker.rb', line 36

def faker_class
  @faker_class
end

#faker_methodMethod (readonly)

Returns The Faker class method.

Returns:

  • (Method)

    The Faker class method



39
40
41
# File 'lib/spec_forge/attribute/faker.rb', line 39

def faker_method
  @faker_method
end

Instance Method Details

#base_objectObject

Returns the base object for the variable chain

Returns:

  • (Object)

    The result of the Faker call



62
63
64
65
66
67
68
69
70
# File 'lib/spec_forge/attribute/faker.rb', line 62

def base_object
  if (positional = arguments[:positional]) && positional.present?
    faker_method.call(*positional.resolved)
  elsif (keyword = arguments[:keyword]) && keyword.present?
    faker_method.call(**keyword.resolved)
  else
    faker_method.call
  end
end