Module: Spectre::Mixin

Defined in:
lib/spectre/mixin.rb

Defined Under Namespace

Classes: MixinContext

Constant Summary collapse

@@mixins =
{}

Class Method Summary collapse

Class Method Details

.mixin(desc, &block) ⇒ Object



28
29
30
# File 'lib/spectre/mixin.rb', line 28

def mixin desc, &block
  @@mixins[desc] = block
end

.run(desc, with: []) ⇒ Object Also known as: also, step



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spectre/mixin.rb', line 32

def run desc, with: []
  raise "no mixin with desc '#{desc}' defined" unless @@mixins.key? desc

  Spectre::Logging.log_debug "running mixin '#{desc}'"

  params = with || {}
  if params.empty?
    params = {}
  end

  ctx = MixinContext.new(desc)

  if params.is_a? Array
    return_val = ctx._execute(*params, &@@mixins[desc])
  elsif params.is_a? Hash
    return_val = ctx._execute(OpenStruct.new(params), &@@mixins[desc])
  else
    return_val = ctx._execute(params, &@@mixins[desc])
  end

  return_val.is_a?(Hash) ? OpenStruct.new(return_val) : return_val
end