Class: Berkshelf::CookbookSource

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

Constant Summary collapse

DEFAULT_CONSTRAINT =
'>= 0.0.0'
@@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

  • :metadata (String)

    use the metadata at the given pat

  • :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)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/berkshelf/cookbook_source.rb', line 107

def initialize(berksfile, name, options = {})
  @options = 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[:constraint] || DEFAULT_CONSTRAINT)

  @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)



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

def berksfile
  @berksfile
end

#cached_cookbookBerkshelf::CachedCookbook



81
82
83
# File 'lib/berkshelf/cookbook_source.rb', line 81

def cached_cookbook
  @cached_cookbook
end

#groupsArray<Symbol> (readonly)

The list of groups this CookbookSource belongs to.

Returns:

  • (Array<Symbol>)


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

def groups
  @groups
end

#locationBerkshelf::Location (readonly)

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:



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

def location
  @location
end

#nameString (readonly)

Returns:



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

def name
  @name
end

#version_constraintSolve::Constraint

Returns:

  • (Solve::Constraint)


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

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


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

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


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

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>)


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

def location_keys
  @@location_keys
end

.valid_optionsArray<Symbol>

Returns an array of valid options to pass to the initializer

Returns:

  • (Array<Symbol>)


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

def valid_options
  @@valid_options
end

.validate_options(options) ⇒ Object



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

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



123
124
125
126
127
128
129
130
# File 'lib/berkshelf/cookbook_source.rb', line 123

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:



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

def cached_and_location(options = {})
  from_path(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)


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

def downloaded?
  !self.cached_cookbook.nil?
end

#has_group?(group) ⇒ Boolean

Returns true if this CookbookSource has the given group.

Returns:

  • (Boolean)


150
151
152
# File 'lib/berkshelf/cookbook_source.rb', line 150

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

#inspectObject



186
187
188
189
190
191
192
193
# File 'lib/berkshelf/cookbook_source.rb', line 186

def inspect
  '#<Berkshelf::CookbookSource: ' << [
    "#{name} (#{version_constraint})",
    "locked_version: #{locked_version ? locked_version.to_s : 'nil'}",
    "groups: #{groups}",
    "location: #{location || 'default'}>"
  ].join(', ')
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



162
163
164
# File 'lib/berkshelf/cookbook_source.rb', line 162

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

#to_hashObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/berkshelf/cookbook_source.rb', line 195

def to_hash
  {}.tap do |h|
    unless location.kind_of?(PathLocation)
      h[:locked_version] = locked_version.to_s
    end

    if location.kind_of?(SiteLocation)
      h[:site] = location.api_uri if location.api_uri != CommunityREST::V1_API
    end

    if location.kind_of?(PathLocation)
      h[:path] = location.relative_path(berksfile.filepath)
    end

    if location.kind_of?(GitLocation)
      h[:git] = location.uri
      h[:ref] = location.ref
      h[:rel] = location.rel if location.rel
    end
  end.reject { |k,v| v.blank? }
end

#to_json(options = {}) ⇒ Object



217
218
219
# File 'lib/berkshelf/cookbook_source.rb', line 217

def to_json(options = {})
  JSON.pretty_generate(to_hash, options)
end

#to_sObject



182
183
184
# File 'lib/berkshelf/cookbook_source.rb', line 182

def to_s
  "#<Berkshelf::CookbookSource: #{name} (#{version_constraint})>"
end