Class: Berkshelf::PathLocation

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

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

#download, 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 (#to_s)

    a filepath to the cookbook on your local disk

  • :metadata (Boolean)

    true if this is a metadata source



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

def initialize(name, version_constraint, options = {})
  @name               = name
  @version_constraint = version_constraint
  @path               = options[:path].to_s
  @metadata           = options[:metadata]
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:



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

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

#cookbookBerkshelf::CachedCookbook

The cookbook associated with this path location.

Returns:



49
50
51
# File 'lib/berkshelf/locations/path_location.rb', line 49

def cookbook
  @cookbook ||= CachedCookbook.from_path(path, name: name)
end

#metadata?Boolean

Returns true if the location is a metadata location. By default, no locations are the metadata location.

Returns:

  • (Boolean)


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

def metadata?
  !!@metadata
end

#relative_path(target = '.') ⇒ String

Return this PathLocation’s path relative to the given target.

Parameters:

  • target (#to_s) (defaults to: '.')

    the path to a file or directory to be relative to

Returns:

  • (String)

    the relative path relative to the target



68
69
70
71
72
73
74
75
76
77
# File 'lib/berkshelf/locations/path_location.rb', line 68

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

#to_hashObject



79
80
81
# File 'lib/berkshelf/locations/path_location.rb', line 79

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

#to_sString

The string representation of this PathLocation. If the path is the default cookbook store, just leave it out, because it’s probably just cached.

Examples:

loc.to_s #=> artifact (1.4.0)
loc.to_s #=> artifact (1.4.0) at path: '/Users/Seth/Dev/artifact'

Returns:



94
95
96
97
98
99
100
# File 'lib/berkshelf/locations/path_location.rb', line 94

def to_s
  if path.to_s.include?(Berkshelf.berkshelf_path.to_s)
    "#{self.class.location_key}"
  else
    "#{self.class.location_key}: '#{path}'"
  end
end