Module: Berkshelf::Location

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

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.



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

def name
  @name
end

#version_constraintObject (readonly)

Returns the value of attribute version_constraint.



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

def version_constraint
  @version_constraint
end

Class Method Details

.included(base) ⇒ Object



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

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:



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

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:



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

def download(destination)
  raise AbstractFunction
end

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

Parameters:

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


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

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

#to_hashObject



159
160
161
162
163
# File 'lib/berkshelf/location.rb', line 159

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

#to_json(options = {}) ⇒ Object



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

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

#validate_cached(cached_cookbook) ⇒ Boolean

TODO:

Change MismatchedCookbookName to raise instead of warn

Ensure the retrieved CachedCookbook is valid

Parameters:

  • cached_cookbook (CachedCookbook)

    the downloaded cookbook to validate

Returns:

  • (Boolean)

Raises:



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/berkshelf/location.rb', line 147

def validate_cached(cached_cookbook)
  unless version_constraint.satisfies?(cached_cookbook.version)
    raise CookbookValidationFailure.new(self, cached_cookbook)
  end

  unless self.name == cached_cookbook.cookbook_name
    Berkshelf.ui.warn(MismatchedCookbookName.new(self, cached_cookbook).to_s)
  end

  true
end