Sha256: 0a24e46fab6c8a0e9a8538ffde2b773ce1d826890386d1da111623872aa0f74b

Contents?: true

Size: 1.29 KB

Versions: 16

Compression:

Stored size: 1.29 KB

Contents

require 'infrataster/resources'
require 'infrataster/contexts/base_context'
require 'infrataster/contexts/no_resource_context'
require 'infrataster/contexts/http_context'
require 'infrataster/contexts/capybara_context'

module Infrataster
  module Contexts
    class << self
      def from_example(example)
        example_group = example.metadata[:example_group]

        server_resource = find_described(Resources::ServerResource, example_group)
        resource = find_described(Resources::BaseResource, example_group)

        unless server_resource || resource
          # There is neither server_resource or resource
          return nil
        end

        if server_resource && !resource
          # Server is found but resource is not found
          return Contexts::NoResourceContext.new(server_resource.server)
        end

        resource.context_class.new(server_resource.server, resource)
      end

      private
      def find_described(resource_class, example_group)
        arg = example_group[:description_args].first
        if arg.is_a?(resource_class)
          arg
        else
          parent_example_group = example_group[:example_group]
          if parent_example_group
            find_described(resource_class, parent_example_group)
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
infrataster-0.2.6 lib/infrataster/contexts.rb
infrataster-0.2.5 lib/infrataster/contexts.rb
infrataster-0.2.4 lib/infrataster/contexts.rb
infrataster-0.2.3 lib/infrataster/contexts.rb
infrataster-0.2.2 lib/infrataster/contexts.rb
infrataster-0.2.1 lib/infrataster/contexts.rb
infrataster-0.2.0 lib/infrataster/contexts.rb
infrataster-0.2.0.beta1 lib/infrataster/contexts.rb
infrataster-0.1.13 lib/infrataster/contexts.rb
infrataster-0.1.12 lib/infrataster/contexts.rb
infrataster-0.1.11 lib/infrataster/contexts.rb
infrataster-0.1.10 lib/infrataster/contexts.rb
infrataster-0.1.9 lib/infrataster/contexts.rb
infrataster-0.1.8 lib/infrataster/contexts.rb
infrataster-0.1.7 lib/infrataster/contexts.rb
infrataster-0.1.6 lib/infrataster/contexts.rb