Class: Berkshelf::CookbookSource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/berkshelf/cookbook_source.rb

Overview

Author:

Constant Summary collapse

@@valid_options =
[:constraint, :locations, :group, :locked_version]
@@location_keys =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(berksfile, name, options = {}) ⇒ CookbookSource

Returns a new instance of CookbookSource.

Parameters:

  • berksfile (Berkshelf::Berksfile)

    the berksfile this source belongs to

  • name (String)

    the name of source

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :constraint (String, Solve::Constraint)

    version constraint to resolve for this source

  • :git (String)

    the Git URL to clone

  • :site (String)

    a URL pointing to a community API endpoint

  • :path (String)

    a filepath to the cookbook on your local disk

  • :group (Symbol, Array)

    the group or groups that the cookbook belongs to

  • :ref (String)

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

  • :branch (String)

    same as ref

  • :tag (String)

    same as tag

  • :locked_version (String)


102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/berkshelf/cookbook_source.rb', line 102

def initialize(berksfile, name, options = {})
  self.class.validate_options(options)

  @berksfile          = berksfile
  @name               = name
  @locked_version     = Solve::Version.new(options[:locked_version]) if options[:locked_version]
  @version_constraint = Solve::Constraint.new(options[:locked_version] || options[:constraint] || ">= 0.0.0")

  @cached_cookbook, @location = cached_and_location(options)

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

Instance Attribute Details

#berksfileBerkshelf::Berksfile (readonly)



72
73
74
# File 'lib/berkshelf/cookbook_source.rb', line 72

def berksfile
  @berksfile
end

#cached_cookbookBerkshelf::CachedCookbook



78
79
80
# File 'lib/berkshelf/cookbook_source.rb', line 78

def cached_cookbook
  @cached_cookbook
end

#nameString (readonly)

Returns:



74
75
76
# File 'lib/berkshelf/cookbook_source.rb', line 74

def name
  @name
end

#version_constraintSolve::Constraint (readonly)

Returns:

  • (Solve::Constraint)


76
77
78
# File 'lib/berkshelf/cookbook_source.rb', line 76

def version_constraint
  @version_constraint
end

Class Method Details

.add_location_key(location, klass) ⇒ Array<Symbol>

Register a location key with the CookbookSource class

Parameters:

  • location (Symbol)

Returns:

  • (Array<Symbol>)

Raises:

  • (ArgumentError)

    if the location key has already been defined

See Also:

  • #location_keys


43
44
45
46
47
48
49
50
# File 'lib/berkshelf/cookbook_source.rb', line 43

def add_location_key(location, klass)
  unless @@location_keys.has_key?(location)
    add_valid_option(location)
    @@location_keys[location] = klass
  end

  @@location_keys
end

.add_valid_option(option) ⇒ Array<Symbol>

Add a option to the list of valid options

Parameters:

  • option (Symbol)

Returns:

  • (Array<Symbol>)

See Also:

  • #valid_options


30
31
32
33
# File 'lib/berkshelf/cookbook_source.rb', line 30

def add_valid_option(option)
  @@valid_options.push(option) unless @@valid_options.include?(option)
  @@valid_options
end

.location_keysArray<Symbol>

Returns an array of the registered source location keys. Every source location is identified by a key (symbol) to differentiate which class to instantiate for the location of a CookbookSource at initialization.

Returns:

  • (Array<Symbol>)


20
21
22
# File 'lib/berkshelf/cookbook_source.rb', line 20

def location_keys
  @@location_keys
end

.valid_optionsArray<Symbol>

Returns an array of valid options to pass to the initializer

Returns:

  • (Array<Symbol>)


11
12
13
# File 'lib/berkshelf/cookbook_source.rb', line 11

def valid_options
  @@valid_options
end

.validate_options(options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/berkshelf/cookbook_source.rb', line 52

def validate_options(options)
  invalid_options = (options.keys - valid_options)

  unless invalid_options.empty?
    invalid_options.collect! { |opt| "'#{opt}'" }
    raise InternalError, "Invalid options for Cookbook Source: #{invalid_options.join(', ')}."
  end

  if (options.keys & [:site, :path, :git]).size > 1
    invalid = (options.keys & [:site, :path, :git]).map { |opt| "'#{opt}" }
    raise InternalError, "Cannot specify #{invalid.to_sentence} for a Cookbook Source!"
  end

  true
end

Instance Method Details

#add_group(*local_groups) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/berkshelf/cookbook_source.rb', line 116

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_and_location(options = {}) ⇒ Array<CachedCookbook, Location>

Determine the CachedCookbook and Location information from the given options.

Returns:



128
129
130
# File 'lib/berkshelf/cookbook_source.rb', line 128

def cached_and_location(options = {})
  from_path(options) || from_cache(options) || from_default(options)
end

#downloaded?Boolean

Returns true if the cookbook source has already been downloaded. A cookbook source is downloaded when a cached cookbook is present.

Returns:

  • (Boolean)


136
137
138
# File 'lib/berkshelf/cookbook_source.rb', line 136

def downloaded?
  !self.cached_cookbook.nil?
end

#groupsArray<Symbol>

The list of groups this CookbookSource belongs to.

Returns:

  • (Array<Symbol>)


171
172
173
# File 'lib/berkshelf/cookbook_source.rb', line 171

def groups
  @groups ||= []
end

#has_group?(group) ⇒ Boolean

Returns true if this CookbookSource has the given group.

Returns:

  • (Boolean)


143
144
145
# File 'lib/berkshelf/cookbook_source.rb', line 143

def has_group?(group)
  groups.include?(group.to_sym)
end

#locationBerkshelf::Location

The location for this CookbookSource, such as a remote Chef Server, the community API, :git, or a :path location. By default, this will be the community API.

Returns:



164
165
166
# File 'lib/berkshelf/cookbook_source.rb', line 164

def location
  @location
end

#locked_versionSolve::Version?

Get the locked version of this cookbook. First check the instance variable and then resort to the cached_cookbook for the version.

This was formerly a delegator, but it would fail if the ‘@cached_cookbook` was nil or undefined.

Returns:

  • (Solve::Version, nil)

    the locked version of this cookbook



155
156
157
# File 'lib/berkshelf/cookbook_source.rb', line 155

def locked_version
  @locked_version ||= cached_cookbook.try(:version)
end

#to_hashObject



181
182
183
184
185
186
187
# File 'lib/berkshelf/cookbook_source.rb', line 181

def to_hash
  {}.tap do |h|
    h[:name]           = self.name
    h[:locked_version] = self.locked_version
    h[:location]       = self.location.to_hash if self.location
  end
end

#to_jsonObject



189
190
191
# File 'lib/berkshelf/cookbook_source.rb', line 189

def to_json
  MultiJson.dump(self.to_hash, pretty: true)
end

#to_sObject



175
176
177
178
179
# File 'lib/berkshelf/cookbook_source.rb', line 175

def to_s
  msg = "#{self.name} (#{self.version_constraint}) groups: #{self.groups}"
  msg << " location: #{self.location}" if self.location
  msg
end