Module: SpecForge::Attribute::Chainable

Included in:
Factory, Faker, Variable
Defined in:
lib/spec_forge/attribute/chainable.rb

Overview

Adds support for an attribute to accept n-number of chained calls. It supports chaining methods, hash keys, and array indexes. It also works well alongside Parameterized attributes

This module requires being included into a class first before it can be used

Examples:

Basic usage in YAML

my_variable: variable.users.first

Advanced usage in YAML

my_variable: variable.users.0.posts.second.author.name

Basic usage in code

faker = SpecForge::Attribute.from("faker.name.name.upcase")
faker.resolved #=> BENDING UNIT 22

Constant Summary collapse

NUMBER_REGEX =

Regular expression that matches pure numeric strings Used for detecting potential array index operations

Returns:

  • (Regexp)

    A case-insensitive regex matching strings containing only digits

/^\d+$/i

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_objectObject (readonly)

The initial object from which the chain will start traversing

Returns:

  • (Object)

    The base object that starts the method/attribute chain



56
57
58
# File 'lib/spec_forge/attribute/chainable.rb', line 56

def base_object
  @base_object
end

#headerSymbol (readonly)

The second part of the chained attribute

Returns:

  • (Symbol)

    The second component of the chained attribute



42
43
44
# File 'lib/spec_forge/attribute/chainable.rb', line 42

def header
  @header
end

#invocation_chainArray<Symbol> (readonly)

The remaining parts of the attribute chain after the header

Returns:

  • (Array<Symbol>)

    The remaining method/key invocations in the chain



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

def invocation_chain
  @invocation_chain
end

#keywordSymbol (readonly)

The first part of the chained attribute

Returns:

  • (Symbol)

    The first component of the chained attribute



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

def keyword
  @keyword
end

Instance Method Details

#initializeObject

Initializes a new chainable attribute by parsing the input into components

Parses the input string into keyword, header, and invocation chain parts.



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

def initialize(...)
  super

  sections = input.split(".")

  @keyword = sections.first.to_sym
  @header = sections.second&.to_sym
  @invocation_chain = sections[2..] || []
end

#resolvedObject

Resolves the chain and stores the result The result is memoized, so subsequent calls return the same value even for dynamic attributes like Faker and Factory

Returns:

  • (Object)

    The fully resolved and memoized value



90
91
92
# File 'lib/spec_forge/attribute/chainable.rb', line 90

def resolved
  @resolved ||= resolve_chain
end

#valueObject

Returns the value of this attribute by resolving the chain Will return a new value on each call for dynamic attributes like Faker

Returns:

  • (Object)

    The result of invoking the chain on the base object



79
80
81
# File 'lib/spec_forge/attribute/chainable.rb', line 79

def value
  invoke_chain
end