Module: Berkshelf::Location

Included in:
ChefAPILocation, GitLocation, PathLocation, SiteLocation
Defined in:
lib/berkshelf/location.rb

Overview

Author:

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

OPSCODE_COMMUNITY_API =
'http://cookbooks.opscode.com/api/v1/cookbooks'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



117
118
119
# File 'lib/berkshelf/location.rb', line 117

def name
  @name
end

#version_constraintObject (readonly)

Returns the value of attribute version_constraint.



118
119
120
# File 'lib/berkshelf/location.rb', line 118

def version_constraint
  @version_constraint
end

Class Method Details

.included(base) ⇒ Object



61
62
63
# File 'lib/berkshelf/location.rb', line 61

def included(base)
  base.send :extend, ClassMethods
end

.init(name, constraint, options = {}) ⇒ SiteLocation, ...

Creates a new instance of a Class implementing Location with the given name and constraint. Which Class to instantiated is determined by the values in the given options Hash. Source Locations have an associated location_key registered with CookbookSource. If your options Hash contains a key matching one of these location_keys then the Class who registered that location_key will be instantiated. If you do not provide an option with a matching location_key a SiteLocation class will be instantiated.

Examples:

Location.init("nginx", ">= 0.0.0", git: "git://github.com/RiotGames/artifact-cookbook.git") =>
  instantiates a GitLocation

Location.init("nginx", ">= 0.0.0", path: "/Users/reset/code/nginx-cookbook") =>
  instantiates a PathLocation

Location.init("nginx", ">= 0.0.0", site: "http://cookbooks.opscode.com/api/v1/cookbooks") =>
  instantiates a SiteLocation

Location.init("nginx", ">= 0.0.0", chef_api: "https://api.opscode.com/organizations/vialstudios") =>
  instantiates a ChefAPILocation

Location.init("nginx", ">= 0.0.0") =>
  instantiates a SiteLocation

Parameters:

  • name (String)
  • constraint (String, Solve::Constraint)
  • options (Hash) (defaults to: {})

Returns:



94
95
96
97
98
# File 'lib/berkshelf/location.rb', line 94

def init(name, constraint, options = {})
  klass = klass_from_options(options)

  klass.new(name, constraint, options)
end

Instance Method Details

#download(destination) ⇒ Berkshelf::CachedCookbook

Parameters:

  • destination (#to_s)

Returns:

Raises:



132
133
134
# File 'lib/berkshelf/location.rb', line 132

def download(destination)
  raise AbstractFunction
end

#downloaded?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/berkshelf/location.rb', line 137

def downloaded?
  @downloaded_status
end

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

Parameters:

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


123
124
125
126
127
# File 'lib/berkshelf/location.rb', line 123

def initialize(name, version_constraint, options = {})
  @name               = name
  @version_constraint = version_constraint
  @downloaded_status  = false
end

#to_hashObject



164
165
166
167
168
# File 'lib/berkshelf/location.rb', line 164

def to_hash
  {
    type: self.class.location_key
  }
end

#to_jsonObject



170
171
172
# File 'lib/berkshelf/location.rb', line 170

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

#validate_cached(cached_cookbook) ⇒ Boolean

Ensure the retrieved CachedCookbook is valid

Parameters:

Returns:

  • (Boolean)

Raises:

  • (ConstraintNotSatisfied)

    if the CachedCookbook does not satisfy the version constraint of this instance of Location.

  • (AmbiguousCookbookName)

    if the CachedCookbook’s name does not match the locations’s name attribute



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/berkshelf/location.rb', line 151

def validate_cached(cached_cookbook)
  unless version_constraint.satisfies?(cached_cookbook.version)
    raise ConstraintNotSatisfied, "A cookbook satisfying '#{self.name}' (#{self.version_constraint}) not found at #{self}"
  end

  # JW TODO: Safe to uncomment when when Opscode makes the 'name' a required attribute in Cookbook metadata
  # unless self.name == cached_cookbook.cookbook_name
  #   raise AmbiguousCookbookName, "Expected a cookbook at #{self} to be named '#{self.name}'. Did you set the 'name' attribute in your Cookbooks metadata? If you didn't, the name of the directory will be used as the name of your Cookbook (awful, right?)."
  # end

  true
end