Sha256: 76a16e0ebf8f7c53f7d8fc184bd398bf446c929a67ac3cc6016ccc72a730b993

Contents?: true

Size: 1.23 KB

Versions: 11

Compression:

Stored size: 1.23 KB

Contents

module Berkshelf
  class Location
    class << self
      # Create a new instance of a Location class given dependency and options.
      # The type of class is determined by the values in the given +options+
      # Hash.
      #
      # If you do not provide an option with a matching location id, +nil+
      # is returned.
      #
      # @example Create a git location
      #   Location.init(dependency, git: 'git://github.com/berkshelf/berkshelf.git')
      #
      # @example Create a GitHub location
      #   Location.init(dependency, github: 'berkshelf/berkshelf')
      #
      # @param [Dependency] dependency
      # @param [Hash] options
      #
      # @return [~BaseLocation, nil]
      def init(dependency, options = {})
        if ( klass = klass_from_options(options) )
          klass.new(dependency, options)
        else
          nil
        end
      end

      private

        # Load the correct location from the given options.
        #
        # @return [Class, nil]
      def klass_from_options(options)
        options.each do |key, _|
          id = key.to_s.capitalize

          begin
            return Berkshelf.const_get("#{id}Location")
          rescue NameError; end
        end

        nil
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
berkshelf-7.0.10 lib/berkshelf/location.rb
berkshelf-7.0.9 lib/berkshelf/location.rb
berkshelf-7.0.8 lib/berkshelf/location.rb
berkshelf-7.0.7 lib/berkshelf/location.rb
berkshelf-7.0.6 lib/berkshelf/location.rb
berkshelf-7.0.5 lib/berkshelf/location.rb
berkshelf-7.0.4 lib/berkshelf/location.rb
berkshelf-7.0.3 lib/berkshelf/location.rb
berkshelf-7.0.2 lib/berkshelf/location.rb
berkshelf-7.0.1 lib/berkshelf/location.rb
berkshelf-7.0.0 lib/berkshelf/location.rb