Class: Synthra::Scenarios::Scenario
- Inherits:
-
Object
- Object
- Synthra::Scenarios::Scenario
- Defined in:
- lib/synthra/scenarios.rb
Overview
A scenario definition
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#fixtures ⇒ Object
readonly
Returns the value of attribute fixtures.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#build(seed: nil) ⇒ ScenarioContext
Build the scenario context.
-
#create(schema, name = nil, **options) ⇒ Object
Define a dependent fixture.
-
#depends_on(scenario_name) ⇒ Object
Declare a dependency on another scenario.
-
#initialize(name, **options) ⇒ Scenario
constructor
A new instance of Scenario.
-
#let(name, schema = nil, **options) { ... } ⇒ FixtureDefinition
Define a named fixture.
-
#let!(name) { ... } ⇒ Object
Define a lazy fixture (evaluated on first access).
- #resolve_build_order ⇒ Object private
-
#setup(&block) ⇒ Object
Add setup block.
-
#teardown(&block) ⇒ Object
Add teardown block.
Constructor Details
#initialize(name, **options) ⇒ Scenario
Returns a new instance of Scenario.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/synthra/scenarios.rb', line 96 def initialize(name, **) @name = name @options = @fixtures = {} @dependencies = [] @setup_blocks = [] @teardown_blocks = [] # Inherit from parent scenario if [:extends] parent = Scenarios[[:extends]] raise ArgumentError, "Unknown parent scenario: #{[:extends]}" unless parent @fixtures = parent.fixtures.dup @dependencies = parent.dependencies.dup end end |
Instance Attribute Details
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
94 95 96 |
# File 'lib/synthra/scenarios.rb', line 94 def dependencies @dependencies end |
#fixtures ⇒ Object (readonly)
Returns the value of attribute fixtures.
94 95 96 |
# File 'lib/synthra/scenarios.rb', line 94 def fixtures @fixtures end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
94 95 96 |
# File 'lib/synthra/scenarios.rb', line 94 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
94 95 96 |
# File 'lib/synthra/scenarios.rb', line 94 def @options end |
Instance Method Details
#build(seed: nil) ⇒ ScenarioContext
Build the scenario context
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/synthra/scenarios.rb', line 172 def build(seed: nil) context = ScenarioContext.new(self, seed: seed) # Load dependencies first @dependencies.each do |dep_name| dep = Scenarios[dep_name] raise ArgumentError, "Unknown dependency: #{dep_name}" unless dep dep_context = dep.build(seed: seed) context.merge!(dep_context) end # Run setup blocks @setup_blocks.each { |block| context.instance_eval(&block) } # Build fixtures in dependency order build_order = resolve_build_order build_order.each do |fixture_name| context.build_fixture(fixture_name) end context end |
#create(schema, name = nil, **options) ⇒ Object
Define a dependent fixture
141 142 143 144 145 146 147 |
# File 'lib/synthra/scenarios.rb', line 141 def create(schema, name = nil, **) name ||= schema.to_s.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase .to_sym @fixtures[name] = FixtureDefinition.new(name, schema, **) end |
#depends_on(scenario_name) ⇒ Object
Declare a dependency on another scenario
153 154 155 |
# File 'lib/synthra/scenarios.rb', line 153 def depends_on(scenario_name) @dependencies << scenario_name end |
#let(name, schema = nil, **options) { ... } ⇒ FixtureDefinition
Define a named fixture
122 123 124 |
# File 'lib/synthra/scenarios.rb', line 122 def let(name, schema = nil, **, &block) @fixtures[name] = FixtureDefinition.new(name, schema, **, &block) end |
#let!(name) { ... } ⇒ Object
Define a lazy fixture (evaluated on first access)
131 132 133 |
# File 'lib/synthra/scenarios.rb', line 131 def let!(name, &block) @fixtures[name] = LazyFixture.new(name, &block) end |
#resolve_build_order ⇒ Object (private)
198 199 200 201 202 |
# File 'lib/synthra/scenarios.rb', line 198 def resolve_build_order # Simple topological sort based on references # For now, just return fixtures in definition order @fixtures.keys end |
#setup(&block) ⇒ Object
Add setup block
158 159 160 |
# File 'lib/synthra/scenarios.rb', line 158 def setup(&block) @setup_blocks << block end |
#teardown(&block) ⇒ Object
Add teardown block
163 164 165 |
# File 'lib/synthra/scenarios.rb', line 163 def teardown(&block) @teardown_blocks << block end |