Class: Synthra::Scenarios::ScenarioContext

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

Overview

Runtime context for a built scenario

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scenario, seed: nil) ⇒ ScenarioContext

Returns a new instance of ScenarioContext.



288
289
290
291
292
293
# File 'lib/synthra/scenarios.rb', line 288

def initialize(scenario, seed: nil)
  @scenario = scenario
  @seed = seed || Random.new_seed
  @data = {}
  @built = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Method missing for fixture access



333
334
335
336
337
338
339
340
341
# File 'lib/synthra/scenarios.rb', line 333

def method_missing(name, *args)
  if @data.key?(name)
    @data[name]
  elsif @scenario.fixtures.key?(name)
    build_fixture(name)
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



286
287
288
# File 'lib/synthra/scenarios.rb', line 286

def data
  @data
end

#scenarioObject (readonly)

Returns the value of attribute scenario.



286
287
288
# File 'lib/synthra/scenarios.rb', line 286

def scenario
  @scenario
end

#seedObject (readonly)

Returns the value of attribute seed.



286
287
288
# File 'lib/synthra/scenarios.rb', line 286

def seed
  @seed
end

Instance Method Details

#[](name) ⇒ Object

Access a fixture by name

Parameters:

  • name (Symbol)

    fixture name

Returns:

  • (Object)

    fixture value



300
301
302
# File 'lib/synthra/scenarios.rb', line 300

def [](name)
  @data[name]
end

#[]=(name, value) ⇒ Object

Set a fixture value



305
306
307
# File 'lib/synthra/scenarios.rb', line 305

def []=(name, value)
  @data[name] = value
end

#build_fixture(name) ⇒ Object

Build a specific fixture



310
311
312
313
314
315
316
317
318
319
# File 'lib/synthra/scenarios.rb', line 310

def build_fixture(name)
  return @data[name] if @built[name]

  fixture_def = @scenario.fixtures[name]
  return nil unless fixture_def

  @data[name] = fixture_def.build(self)
  @built[name] = true
  @data[name]
end

#merge!(other_context) ⇒ Object

Merge another context



322
323
324
325
# File 'lib/synthra/scenarios.rb', line 322

def merge!(other_context)
  @data.merge!(other_context.data)
  @built.merge!(other_context.instance_variable_get(:@built))
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


343
344
345
# File 'lib/synthra/scenarios.rb', line 343

def respond_to_missing?(name, include_private = false)
  @data.key?(name) || @scenario.fixtures.key?(name) || super
end

#to_hObject

Convert to hash



328
329
330
# File 'lib/synthra/scenarios.rb', line 328

def to_h
  @data.dup
end