module Cukedep::HookDSL

Mix-in module that defines the DSL (Domain-Specific Language) for specifying Cukedep hooks. A hook is a custom code block that is executed when a pre-defined Cukedep event occurs.

Constants

ValidHookScopes

Attributes

after_hooks[R]
before_hooks[R]

Public Instance Methods

after_cuke(aScope, &aBlock) click to toggle source

This method registers the code block to execute before a Cucumber invocation.

# File lib/cukedep/hook-dsl.rb, line 26
def after_cuke(aScope, &aBlock)
  kind = :after
  scope = validated_scope(kind, aScope)
  register_hook(kind, scope, aBlock) if block_given?
end
before_cuke(aScope, &aBlock) click to toggle source

This method registers the code block to execute before a Cucumber invocation.

# File lib/cukedep/hook-dsl.rb, line 18
def before_cuke(aScope, &aBlock)
  kind = :before
  scope = validated_scope(kind, aScope)
  register_hook(kind, scope, aBlock) if block_given?
end

Private Instance Methods

handler_for(aKind, aScope) click to toggle source
# File lib/cukedep/hook-dsl.rb, line 66
def handler_for(aKind, aScope)
  if aKind == :before
    hooks = before_hooks
  else
    hooks = after_hooks
  end

  handler = hooks.nil? ? nil : hooks.fetch(aScope)
  return handler
end
register_hook(aKind, aScope, aBlock) click to toggle source

# Execute the specific hook. def execute_hook(aKind, aScope)

scope = validated_scope(aKind, aScope)
case [aKind, scope]
when [:before, :all],  [:before, :each], [:after, :each], [:after, :all]
  handler = handler_for(aKind, scope)
else
  raise StandardError, "Unknown Cukedep hook #{aKind}, #{aScope}"
end

handler.call unless handler.nil?

end

# File lib/cukedep/hook-dsl.rb, line 49
def register_hook(aKind, aScope, aBlock)
  scope = validated_scope(aKind, aScope)

  ivar = "@#{aKind}_hooks".to_sym
  instance_variable_set(ivar, {}) if instance_variable_get(ivar).nil?
  instance_variable_get(ivar)[scope] = aBlock
end
validated_scope(aKind, aScope) click to toggle source
# File lib/cukedep/hook-dsl.rb, line 57
def validated_scope(aKind, aScope)
  unless ValidHookScopes.include?(aScope)
    msg = "Unknown scope '#{aScope}' for #{aKind}_cuke hook."
    raise StandardError, msg
  end

  return aScope
end