class Berkshelf::Dependency

Attributes

berksfile[R]

@return [Berkshelf::Berksfile]

groups[W]

@return [Array<String,Symbol>]

location[R]

@return [Berkshelf::Location]

locked_version[R]

@return [Semverse::Version]

name[R]

@return [String]

source[RW]

@return [Source]

version_constraint[R]

@return [Semverse::Constraint]

Public Class Methods

name(dependency) click to toggle source

Returns the name of this cookbook (because it’s the key in hash tables).

@param [Dependency, to_s] dependency

the dependency to find the name from

@return [String]

the name of the cookbook
# File lib/berkshelf/dependency.rb, line 11
def name(dependency)
  if dependency.is_a?(Dependency)
    dependency.name.to_s
  else
    dependency.to_s
  end
end
new(berksfile, name, options = {}) click to toggle source

@param [Berkshelf::Berksfile] berksfile

the berksfile this dependency belongs to

@param [String] name

the name of dependency

@option options [String, Semverse::Constraint] :constraint

version constraint for this dependency

@option options [String] :git

the Git URL to clone

@option options [String] :path

a filepath to the cookbook on your local disk

@option options [String] :metadata

use the metadata at the given pat

@option options [Symbol, Array] :group

the group or groups that the cookbook belongs to

@option options [String] :ref

the commit hash or an alias to a commit hash to clone

@option options [String] :branch

same as ref

@option options [String] :tag

same as tag

@option options [String] :locked_version

# File lib/berkshelf/dependency.rb, line 57
def initialize(berksfile, name, options = {})
  @options            = options
  @berksfile          = berksfile
  @name               = name
  @metadata           = options[:metadata]
  @location           = Location.init(self, options)

  if options[:locked_version]
    @locked_version = Semverse::Version.coerce(options[:locked_version])
  end

  # The coerce method automatically gives us a default constraint of
  # >= 0.0.0 if the constraint is not set.
  @version_constraint = Semverse::Constraint.coerce(options[:constraint])

  add_group(options[:group]) if options[:group]
  add_group(:default) if groups.empty?
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/berkshelf/dependency.rb, line 161
def <=>(other)
  [name, version_constraint] <=> [other.name, other.version_constraint]
end
add_group(*local_groups) click to toggle source
# File lib/berkshelf/dependency.rb, line 99
def add_group(*local_groups)
  local_groups = local_groups.first if local_groups.first.is_a?(Array)

  local_groups.each do |group|
    group = group.to_sym
    groups << group unless groups.include?(group)
  end
end
cached_cookbook() click to toggle source

Attempt to load the cached_cookbook for this dependency. For SCM/path locations, this method delegates to {BaseLocation#cached_cookbook}. For generic dependencies, this method tries attemps to load a matching cookbook from the {CookbookStore}.

@return [CachedCookbook, nil]

# File lib/berkshelf/dependency.rb, line 122
def cached_cookbook
  return @cached_cookbook if @cached_cookbook

  @cached_cookbook =
    if location
      cookbook = location.cached_cookbook

      # If we have a cached cookbook, tighten our constraints
      if cookbook
        self.locked_version     = cookbook.version
        self.version_constraint = cookbook.version
      end

      cookbook
    else
      if locked_version
        CookbookStore.instance.cookbook(name, locked_version)
      else
        CookbookStore.instance.satisfy(name, version_constraint)
      end
    end

  @cached_cookbook
end
groups() click to toggle source

The list of groups this dependency belongs to.

@return [Array<Symbol>]

# File lib/berkshelf/dependency.rb, line 157
def groups
  @groups ||= []
end
has_group?(group) click to toggle source

Returns true if this dependency has the given group.

@return [Boolean]

# File lib/berkshelf/dependency.rb, line 150
def has_group?(group)
  groups.include?(group.to_sym)
end
inspect() click to toggle source
# File lib/berkshelf/dependency.rb, line 169
def inspect
  "#<Berkshelf::Dependency: " << [
    "#{name} (#{version_constraint})",
    "locked_version: #{locked_version.inspect}",
    "groups: #{groups}",
    "location: #{location || "default"}>",
  ].join(", ")
end
installed?() click to toggle source

Determine if this dependency is installed. A dependency is “installed” if the associated {CachedCookbook} exists on disk.

@return [Boolean]

# File lib/berkshelf/dependency.rb, line 112
def installed?
  !cached_cookbook.nil?
end
locked_version=(version) click to toggle source

Set this dependency’s locked version.

@param [#to_s] version

the version to set
# File lib/berkshelf/dependency.rb, line 87
def locked_version=(version)
  @locked_version = Semverse::Version.coerce(version)
end
metadata?() click to toggle source

Return true if this is a metadata location.

@return [Boolean]

# File lib/berkshelf/dependency.rb, line 79
def metadata?
  !!@metadata
end
to_lock() click to toggle source
# File lib/berkshelf/dependency.rb, line 178
def to_lock
  out =
    if location || version_constraint.to_s == ">= 0.0.0"
      "  #{name}\n"
    else
      "  #{name} (#{version_constraint})\n"
    end

  out << location.to_lock if location
  out
end
to_s() click to toggle source
# File lib/berkshelf/dependency.rb, line 165
def to_s
  "#{name} (#{locked_version || version_constraint})"
end
version_constraint=(constraint) click to toggle source

Set this dependency’s constraint(s).

@param [#to_s] constraint

the constraint to set
# File lib/berkshelf/dependency.rb, line 95
def version_constraint=(constraint)
  @version_constraint = Semverse::Constraint.coerce(constraint)
end