Class: SpecForge::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/spec_forge/configuration.rb

Overview

Holds configuration options for SpecForge

Configuration is typically set in forge_helper.rb using the configure block. It controls base URL, global variables, factory settings, callbacks, and more.

Examples:

Basic configuration

SpecForge.configure do |config|
  config.base_url = "http://localhost:3000"
  config.global_variables = {api_version: "v1"}
end

Defined Under Namespace

Classes: Factories

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Creates a new Configuration with default values



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/spec_forge/configuration.rb', line 53

def initialize
  # Validated
  @base_url = "http://localhost:3000"
  @factories = Factories.new
  @global_variables = {}

  # Internal
  @on_debug_proc = nil
  @callbacks = {}
  @hooks = {
    before_forge: [],
    before_blueprint: [],
    before_step: [],
    after_step: [],
    after_blueprint: [],
    after_forge: []
  }
end

Instance Attribute Details

#base_urlString

Returns Base URL for HTTP requests.

Returns:

  • (String)

    Base URL for HTTP requests



31
32
33
# File 'lib/spec_forge/configuration.rb', line 31

def base_url
  @base_url
end

#callbacksHash{Symbol => Proc} (readonly)

Returns Registered callbacks.

Returns:

  • (Hash{Symbol => Proc})

    Registered callbacks



43
44
45
# File 'lib/spec_forge/configuration.rb', line 43

def callbacks
  @callbacks
end

#factoriesFactories (readonly)

Returns Factory configuration.

Returns:



37
38
39
# File 'lib/spec_forge/configuration.rb', line 37

def factories
  @factories
end

#global_variablesHash

Returns Global variables available to all blueprints.

Returns:

  • (Hash)

    Global variables available to all blueprints



34
35
36
# File 'lib/spec_forge/configuration.rb', line 34

def global_variables
  @global_variables
end

#hooksHash{Symbol => Array} (readonly)

Returns Global lifecycle hooks for forge, blueprint, and step events.

Returns:

  • (Hash{Symbol => Array})

    Global lifecycle hooks for forge, blueprint, and step events



46
47
48
# File 'lib/spec_forge/configuration.rb', line 46

def hooks
  @hooks
end

#on_debug_procProc? (readonly)

Returns Debug handler proc.

Returns:

  • (Proc, nil)

    Debug handler proc



40
41
42
# File 'lib/spec_forge/configuration.rb', line 40

def on_debug_proc
  @on_debug_proc
end

Instance Method Details

#after(event, callback_name = nil) {|context| ... } ⇒ String, Symbol

Attaches a callback to an after lifecycle event

Global hooks run for all blueprints and execute in registration order. Can either reference a pre-registered callback by name, or accept a block to register and attach a callback in one step (like RSpec's after hooks).

Examples:

Attach a pre-registered callback

config.register_callback(:cleanup) { |context| Database.clean }
config.after(:forge, :cleanup)

Register and attach with a block (like RSpec)

config.after(:step) { |context| Logger.info("Step complete") }
config.after(:blueprint) { |context| Database.rollback }

Store the callback name for later deregistration

callback_name = config.after(:step) { |context| puts "done" }
config.deregister_callback(callback_name)

Parameters:

  • event (Symbol)

    The lifecycle event (:forge, :blueprint, or :step)

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

    The name of a registered callback (optional if block is provided)

Yields:

  • (context)

    Block to execute (registers callback automatically)

Yield Parameters:

Returns:

  • (String, Symbol)

    The callback name (auto-generated if block provided)

Raises:

  • (ArgumentError)

    If the event is invalid

  • (ArgumentError)

    If the callback is not registered (when using name)



234
235
236
237
238
239
240
241
242
243
# File 'lib/spec_forge/configuration.rb', line 234

def after(event, callback_name = nil, &block)
  if block
    callback_name = "__sf_cb_#{SecureRandom.uuid.tr("-", "")}"
    register_callback(callback_name, &block)
  end

  register_hook("after", event, callback_name)

  callback_name
end

#before(event, callback_name = nil) {|context| ... } ⇒ String, Symbol

Attaches a callback to a before lifecycle event

Global hooks run for all blueprints and execute in registration order. Can either reference a pre-registered callback by name, or accept a block to register and attach a callback in one step (like RSpec's before hooks).

Examples:

Attach a pre-registered callback

config.register_callback(:setup) { |context| Database.seed }
config.before(:forge, :setup)

Register and attach with a block (like RSpec)

config.before(:step) { |context| Logger.info("Starting step") }
config.before(:blueprint) { |context| Database.clean }

Store the callback name for later deregistration

callback_name = config.before(:step) { |context| puts "hook" }
config.deregister_callback(callback_name)

Parameters:

  • event (Symbol)

    The lifecycle event (:forge, :blueprint, or :step)

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

    The name of a registered callback (optional if block is provided)

Yields:

  • (context)

    Block to execute (registers callback automatically)

Yield Parameters:

Returns:

  • (String, Symbol)

    The callback name (auto-generated if block provided)

Raises:

  • (ArgumentError)

    If the event is invalid

  • (ArgumentError)

    If the callback is not registered (when using name)



192
193
194
195
196
197
198
199
200
201
# File 'lib/spec_forge/configuration.rb', line 192

def before(event, callback_name = nil, &block)
  if block
    callback_name = "__sf_cb_#{SecureRandom.uuid.tr("-", "")}"
    register_callback(callback_name, &block)
  end

  register_hook("before", event, callback_name)

  callback_name
end

#deregister_callback(name) ⇒ Proc?

Removes a registered callback and detaches it from all lifecycle hooks

Examples:

Remove a callback

config.register_callback(:my_hook) { |context| puts "hook" }
config.before(:step, :my_hook)
config.deregister_callback(:my_hook)  # Removes from callbacks and hooks

Parameters:

  • name (String, Symbol)

    The callback name to remove

Returns:

  • (Proc, nil)

    The removed callback proc, or nil if not found



152
153
154
155
156
157
158
159
# File 'lib/spec_forge/configuration.rb', line 152

def deregister_callback(name)
  name = name.to_sym

  callback = @callbacks.delete(name)
  @hooks.each_value { |a| a.delete(name) }

  callback
end

#on_debug {|context| ... } ⇒ Object

Sets a debug handler block to be called when a step has debug: true

Examples:

config.on_debug { binding.pry }

Yields:

  • (context)

    Block called when debug is triggered

Yield Parameters:



105
106
107
# File 'lib/spec_forge/configuration.rb', line 105

def on_debug(&block)
  @on_debug_proc = block
end

#register_callback(name) {|context, *args| ... } ⇒ Object

Registers a callback that can be invoked from blueprints using call:

Examples:

Simple callback

config.register_callback("seed_data") do |context|
  User.create!(name: "Test")
end

Callback with arguments

config.register_callback("create_users") do |context, count:|
  count.times { User.create! }
end

Parameters:

  • name (String, Symbol)

    The callback name to register

Yields:

  • (context, *args)

    Block to execute when callback is called

Yield Parameters:



136
137
138
# File 'lib/spec_forge/configuration.rb', line 136

def register_callback(name, &block)
  @callbacks[name.to_sym] = block
end

#rspecRSpec::Core::Configuration

Returns RSpec's configuration for customization

Returns:

  • (RSpec::Core::Configuration)

    RSpec configuration



114
115
116
# File 'lib/spec_forge/configuration.rb', line 114

def rspec
  RSpec.configuration
end

#validateConfiguration

Validates the configuration and normalizes values

Returns:

Raises:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/spec_forge/configuration.rb', line 79

def validate
  output = Normalizer.normalize!(
    {
      base_url: @base_url,
      factories: @factories.to_h,
      global_variables: @global_variables
    },
    using: :configuration
  )

  # In case any value was set to `nil`
  @global_variables = output[:global_variables] if @global_variables.blank?
  @global_variables.symbolize_keys!

  self
end