Module: SpecForge::Attribute::Chainable
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
Constant Summary collapse
- NUMBER_REGEX =
Regular expression that matches pure numeric strings Used for detecting potential array index operations
/^\d+$/i
Instance Attribute Summary collapse
-
#base_object ⇒ Object
readonly
The initial object from which the chain will start traversing.
-
#header ⇒ Symbol
readonly
The second part of the chained attribute.
-
#invocation_chain ⇒ Array<Symbol>
readonly
The remaining parts of the attribute chain after the header.
-
#keyword ⇒ Symbol
readonly
The first part of the chained attribute.
Instance Method Summary collapse
-
#initialize ⇒ Object
Initializes a new chainable attribute by parsing the input into components.
-
#resolved ⇒ Object
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.
-
#value ⇒ Object
Returns the value of this attribute by resolving the chain Will return a new value on each call for dynamic attributes like Faker.
Instance Attribute Details
#base_object ⇒ Object (readonly)
The initial object from which the chain will start traversing
56 57 58 |
# File 'lib/spec_forge/attribute/chainable.rb', line 56 def base_object @base_object end |
#header ⇒ Symbol (readonly)
The second part of the chained attribute
42 43 44 |
# File 'lib/spec_forge/attribute/chainable.rb', line 42 def header @header end |
#invocation_chain ⇒ Array<Symbol> (readonly)
The remaining parts of the attribute chain after the header
49 50 51 |
# File 'lib/spec_forge/attribute/chainable.rb', line 49 def invocation_chain @invocation_chain end |
#keyword ⇒ Symbol (readonly)
The first part of the chained attribute
35 36 37 |
# File 'lib/spec_forge/attribute/chainable.rb', line 35 def keyword @keyword end |
Instance Method Details
#initialize ⇒ Object
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 |
#resolved ⇒ Object
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
90 91 92 |
# File 'lib/spec_forge/attribute/chainable.rb', line 90 def resolved @resolved ||= resolve_chain end |
#value ⇒ Object
Returns the value of this attribute by resolving the chain Will return a new value on each call for dynamic attributes like Faker
79 80 81 |
# File 'lib/spec_forge/attribute/chainable.rb', line 79 def value invoke_chain end |