Sha256: 3202bfde433edf2261d12bcd7a856faab0da857b47ef48309e4c3f7f31c4ee18

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module Berkshelf
  # @author Jamie Winsor <reset@riotgames.com>
  class PathLocation
    class << self
      # Expand and return a string representation of the given path if it is
      # absolute or a path in the users home directory.
      #
      # Returns the given relative path otherwise.
      #
      # @param [#to_s] path
      #
      # @return [String]
      def normalize_path(path)
        path = path.to_s
        if (path[0] == "~") || Pathname.new(path).absolute?
          File.expand_path(path)
        else
          path
        end
      end
    end

    include Location

    set_location_key :path

    attr_accessor :path

    # @param [#to_s] name
    # @param [Solve::Constraint] version_constraint
    # @param [Hash] options
    #
    # @option options [String] :path
    #   a filepath to the cookbook on your local disk
    def initialize(name, version_constraint, options = {})
      @name               = name
      @version_constraint = version_constraint
      @path               = self.class.normalize_path(options[:path])
      set_downloaded_status(true)
    end

    # @param [#to_s] destination
    #
    # @return [Berkshelf::CachedCookbook]
    def download(destination)
      cached = CachedCookbook.from_path(File.expand_path(path))
      validate_cached(cached)

      set_downloaded_status(true)
      cached
    end

    def to_hash
      super.merge(value: self.path)
    end

    def to_s
      "#{self.class.location_key}: '#{path}'"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
berkshelf-1.2.0.rc1 lib/berkshelf/locations/path_location.rb