Class: Synthra::Scenarios::Scenario

Inherits:
Object
  • Object
show all
Defined in:
lib/synthra/scenarios.rb

Overview

A scenario definition

Instance Attribute Summary collapse

Instance Method Summary collapse

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, **options)
  @name = name
  @options = options
  @fixtures = {}
  @dependencies = []
  @setup_blocks = []
  @teardown_blocks = []

  # Inherit from parent scenario
  if options[:extends]
    parent = Scenarios[options[:extends]]
    raise ArgumentError, "Unknown parent scenario: #{options[:extends]}" unless parent

    @fixtures = parent.fixtures.dup
    @dependencies = parent.dependencies.dup
  end
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



94
95
96
# File 'lib/synthra/scenarios.rb', line 94

def dependencies
  @dependencies
end

#fixturesObject (readonly)

Returns the value of attribute fixtures.



94
95
96
# File 'lib/synthra/scenarios.rb', line 94

def fixtures
  @fixtures
end

#nameObject (readonly)

Returns the value of attribute name.



94
95
96
# File 'lib/synthra/scenarios.rb', line 94

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



94
95
96
# File 'lib/synthra/scenarios.rb', line 94

def options
  @options
end

Instance Method Details

#build(seed: nil) ⇒ ScenarioContext

Build the scenario context

Parameters:

  • seed (Integer) (defaults to: nil)

    random seed for determinism

Returns:



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

Parameters:

  • schema (String, Symbol)

    schema to generate

  • name (Symbol) (defaults to: nil)

    fixture name

  • options (Hash)

    generation options with references



141
142
143
144
145
146
147
# File 'lib/synthra/scenarios.rb', line 141

def create(schema, name = nil, **options)
  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, **options)
end

#depends_on(scenario_name) ⇒ Object

Declare a dependency on another scenario

Parameters:

  • scenario_name (Symbol)

    name of scenario to include



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

Parameters:

  • name (Symbol)

    fixture name

  • schema (String, Symbol) (defaults to: nil)

    schema name (optional)

  • options (Hash)

    generation options

Yields:

  • optional customization block

Returns:



122
123
124
# File 'lib/synthra/scenarios.rb', line 122

def let(name, schema = nil, **options, &block)
  @fixtures[name] = FixtureDefinition.new(name, schema, **options, &block)
end

#let!(name) { ... } ⇒ Object

Define a lazy fixture (evaluated on first access)

Parameters:

  • name (Symbol)

    fixture name

Yields:

  • block that returns the fixture value



131
132
133
# File 'lib/synthra/scenarios.rb', line 131

def let!(name, &block)
  @fixtures[name] = LazyFixture.new(name, &block)
end

#resolve_build_orderObject (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