Class: HasHelpers::FactoryScenario::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/has_helpers/factory_scenario/dsl.rb

Overview

DSL used by FactoryScenario.define to declare reusable e2e setups.

Examples

HasHelpers::FactoryScenario.define "basic/example_setup" do # Pull in lets/helpers from an existing RSpec shared context. include_context "some_shared_context"

# Declare lazy aliases that can be activated by run/response/request refs.
build :organization
build :policy, default: { organization: "@organization" }
build :advisor, as: :writing_advisor, traits: [:with_org]

# Expose a shared-context "let" so `@product_type` can be used in request attrs.
expose :product_type

# Optional execution hook; return hash keys are default response aliases.
run do
  perform_some_action!(policy)

  { policy: policy, organization: organization }
end
end

Client request (example):
{
"scenario": "basic/example_setup",
"objects": {
  "organization": { "attributes": { "name": "my_org" } },
  "custom_policy": {
    "factory": "policy",
    "attributes": { "organization": "@organization", "product_type": "@product_type" }
  }
},
"response": ["organization", "policy", "custom_policy"]
}

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ DSL

Returns a new instance of DSL.



42
43
44
# File 'lib/has_helpers/factory_scenario/dsl.rb', line 42

def initialize(definition)
  @definition = definition
end

Instance Method Details

#build(factory, as: nil, traits: [], default: {}, &block) ⇒ Object

Declare a lazy alias backed by a factory.

params:

  • factory <Symbol|String> FactoryBot factory name (e.g. :policy).

  • as <Symbol|String|nil> Alias used by the scenario's reference system (e.g. "custom_policy"). Defaults to factory.

  • traits Array<Symbol|String> Traits applied when the object is built.

  • default Hash Default attributes merged before request overrides. Passed to factory bot. Supports "@alias" references (e.g. { organization: "@organization" }).

  • block <Proc|nil> Reserved hook for future composition/extensions.



60
61
62
# File 'lib/has_helpers/factory_scenario/dsl.rb', line 60

def build(factory, as: nil, traits: [], default: {}, &block)
  @definition.build(factory, as: as, traits: traits, default: default, &block)
end

#expose(*aliases) ⇒ Object

Expose shared-context "let"s as aliases usable in @alias references.



70
71
72
# File 'lib/has_helpers/factory_scenario/dsl.rb', line 70

def expose(*aliases)
  @definition.exposed_aliases.concat(aliases.flatten.map(&:to_sym))
end

#include_context(name) ⇒ Object

Attach an RSpec shared context used while evaluating run/exposed aliases.



65
66
67
# File 'lib/has_helpers/factory_scenario/dsl.rb', line 65

def include_context(name)
  @definition.include_contexts << name
end

#run(&block) ⇒ Object

Register executable setup logic; returned hash drives default response payload.



75
76
77
# File 'lib/has_helpers/factory_scenario/dsl.rb', line 75

def run(&block)
  @definition.run(&block)
end