File: collection.rb

package info (click to toggle)
ruby-rspec-core 2.14.7-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,756 kB
  • ctags: 1,195
  • sloc: ruby: 12,708; makefile: 14
file content (43 lines) | stat: -rw-r--r-- 1,213 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module RSpec
  module Core
    module SharedExampleGroup
      class Collection

        def initialize(sources, examples)
          @sources, @examples = sources, examples
        end

        def [](key)
          fetch_examples(key) || warn_deprecation_and_fetch_anyway(key)
        end

        private

          def fetch_examples(key)
            @examples[source_for key][key]
          end

          def source_for(key)
            @sources.reverse.find { |source| @examples[source].has_key? key }
          end

          def fetch_anyway(key)
            @examples.values.inject({}, &:merge)[key]
          end

          def warn_deprecation_and_fetch_anyway(key)
            if (example = fetch_anyway key)
              backtrace_line = caller.find { |line| !line.include?('lib/rspec/core') }
              RSpec.warn_deprecation <<-WARNING.gsub(/^ /, '')
                Accessing shared_examples defined across contexts is deprecated.
                Please declare shared_examples within a shared context, or at the top level.
                This message was generated at: #{backtrace_line}
              WARNING
              example
            end
          end

      end
    end
  end
end