lib/berkshelf/locations/path_location.rb in berkshelf-2.0.3 vs lib/berkshelf/locations/path_location.rb in berkshelf-2.0.4
- old
+ new
@@ -20,37 +20,61 @@
end
include Location
set_location_key :path
+ set_valid_options :path, :metadata
attr_accessor :path
attr_reader :name
# @param [#to_s] name
# @param [Solve::Constraint] version_constraint
# @param [Hash] options
#
- # @option options [String] :path
+ # @option options [#to_s] :path
# a filepath to the cookbook on your local disk
+ # @option options [Boolean] :metadata
+ # true if this is a metadata source
def initialize(name, version_constraint, options = {})
@name = name
@version_constraint = version_constraint
- @path = options[:path]
- set_downloaded_status(true)
+ @path = options[:path].to_s
+ @metadata = options[:metadata]
end
- # @param [#to_s] destination
+ # The cookbook associated with this path location.
#
# @return [Berkshelf::CachedCookbook]
- def download(destination)
- cached = CachedCookbook.from_path(path, name: name)
- validate_cached(cached)
+ # the cached cookbook for this location
+ def cookbook
+ @cookbook ||= CachedCookbook.from_path(path, name: name)
+ end
- set_downloaded_status(true)
- cached
- rescue IOError
- raise Berkshelf::CookbookNotFound
+ # Returns true if the location is a metadata location. By default, no
+ # locations are the metadata location.
+ #
+ # @return [Boolean]
+ def metadata?
+ !!@metadata
+ end
+
+ # Return this PathLocation's path relative to the given target.
+ #
+ # @param [#to_s] target
+ # the path to a file or directory to be relative to
+ #
+ # @return [String]
+ # the relative path relative to the target
+ def relative_path(target = '.')
+ my_path = Pathname.new(path).expand_path
+ target_path = Pathname.new(target.to_s).expand_path
+ target_path = target_path.dirname if target_path.file?
+
+ new_path = my_path.relative_path_from(target_path).to_s
+
+ return new_path if new_path.index('.') == 0
+ "./#{new_path}"
end
def to_hash
super.merge(value: self.path)
end