Sha256: f45a12c2ba9983f93048e597b1b04fda332911ddced39e03174f4fdbb42970e3

Contents?: true

Size: 1.77 KB

Versions: 16

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Support
    module Gems
      ##
      # @api private
      #
      class RSpec
        class << self
          ##
          # @return [Boolean]
          #
          # @internal
          #   `Style/TernaryParentheses` is disabled since `defined?` has too low priority without parentheses.
          #
          # rubocop:disable Style/TernaryParentheses
          def loaded?
            (defined? ::RSpec) ? true : false
          end
          # rubocop:enable Style/TernaryParentheses

          ##
          # @return [ConvenientService::Support::Version]
          #
          # @internal
          #   https://github.com/rspec/rspec-core/blob/main/lib/rspec/core/version.rb
          #
          def version
            loaded? ? Support::Version.new(::RSpec::Core::Version::STRING) : Support::Version.null_version
          end

          ##
          # @return [RSpec::Core::Example, nil]
          #
          # @internal
          #   NOTE: Returns `nil` in environments where RSpec is NOT fully loaded, e.g: irb, rails console, etc.
          #
          #   `::RSpec.current_example` docs:
          #   - https://www.rubydoc.info/github/rspec/rspec-core/RSpec.current_example
          #   - https://github.com/rspec/rspec-core/blob/v3.12.0/lib/rspec/core.rb#L122
          #   - https://relishapp.com/rspec/rspec-core/docs/metadata/current-example
          #
          def current_example
            return unless loaded?

            ##
            # NOTE: This happens in Ruby-only projects where RSpec is loaded by `Bundler.require`, not by `bundle exec rspec`.
            #
            return unless ::RSpec.respond_to?(:current_example)

            ::RSpec.current_example
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
convenient_service-0.18.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.17.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.16.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.15.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.14.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.13.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.12.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.11.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.10.1 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.10.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.9.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.8.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.7.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.6.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.5.0 lib/convenient_service/support/gems/rspec.rb
convenient_service-0.4.0 lib/convenient_service/support/gems/rspec.rb