32: def share_as(name, &block)
33: if Object.const_defined?(name)
34: mod = Object.const_get(name)
35: raise_name_error unless mod.created_from_caller(caller)
36: end
37:
38: mod = Module.new do
39: @shared_block = block
40: @caller_line = caller.last
41:
42: def self.created_from_caller(other_caller)
43: @caller_line == other_caller.last
44: end
45:
46: def self.included(kls)
47: kls.describe(&@shared_block)
48: kls.children.first.metadata[:shared_group_name] = name
49: end
50: end
51:
52: shared_const = Object.const_set(name, mod)
53: RSpec.world.shared_example_groups[shared_const] = block
54: end