Class: ActiveFactory::ContextExtension

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

Overview

introduces models’ names in a spec’s context

Instance Method Summary collapse

Instance Method Details

#extend_test_context(containers_hash, context) ⇒ Object



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/active_factory.rb', line 432

def extend_test_context containers_hash, context
  mrg = proc {|args, hash|
    if args.none?
      hash
    elsif args.one? and args[0].is_a? Hash
      hash.merge args[0]
    else
      raise "Only has is valid argument, but *args=#{args.inspect}"
    end
  }

  method_defs =
      containers_hash.map { |name, container| [
          name, proc { container.singleton.objects[0] },
          :"#{name}_", proc { |*args| mrg[args, container.singleton.attrs[0]] },
          :"#{name.to_s.pluralize}", proc { container.objects },
          :"#{name.to_s.pluralize}_", proc { container.attrs }
      ] }.
          flatten.each_slice(2)

  @undo_define_methods =
      define_methods_with_undo context, method_defs
end

#undoObject



427
428
429
430
# File 'lib/active_factory.rb', line 427

def undo
  @undo_define_methods[] if @undo_define_methods
  @undo_define_methods = nil
end