Class: Berkshelf::PathLocation

Inherits:
Object
  • Object
show all
Includes:
Location
Defined in:
lib/berkshelf/locations/path_location.rb

Overview

Author:

Constant Summary

Constants included from Location

Location::OPSCODE_COMMUNITY_API

Instance Attribute Summary collapse

Attributes included from Location

#version_constraint

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Location

#downloaded?, included, init, #to_json, #validate_cached

Constructor Details

#initialize(name, version_constraint, options = {}) ⇒ PathLocation

Returns a new instance of PathLocation.

Parameters:

  • name (#to_s)
  • version_constraint (Solve::Constraint)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :path (String)

    a filepath to the cookbook on your local disk



36
37
38
39
40
41
# File 'lib/berkshelf/locations/path_location.rb', line 36

def initialize(name, version_constraint, options = {})
  @name               = name
  @version_constraint = version_constraint
  @path               = options[:path]
  set_downloaded_status(true)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/berkshelf/locations/path_location.rb', line 28

def name
  @name
end

#pathObject

Returns the value of attribute path.



27
28
29
# File 'lib/berkshelf/locations/path_location.rb', line 27

def path
  @path
end

Class Method Details

.normalize_path(path) ⇒ String

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.

Parameters:

Returns:



13
14
15
16
17
18
19
20
# File 'lib/berkshelf/locations/path_location.rb', line 13

def normalize_path(path)
  path = path.to_s
  if (path[0] == "~") || Pathname.new(path).absolute?
    File.expand_path(path)
  else
    path
  end
end

Instance Method Details

#download(destination) ⇒ Berkshelf::CachedCookbook

Parameters:

  • destination (#to_s)

Returns:



46
47
48
49
50
51
52
53
54
# File 'lib/berkshelf/locations/path_location.rb', line 46

def download(destination)
  cached = CachedCookbook.from_path(path, name: name)
  validate_cached(cached)

  set_downloaded_status(true)
  cached
rescue IOError
  raise Berkshelf::CookbookNotFound
end

#to_hashObject



56
57
58
# File 'lib/berkshelf/locations/path_location.rb', line 56

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

#to_sObject



60
61
62
# File 'lib/berkshelf/locations/path_location.rb', line 60

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