Class: Berkshelf::CookbookSource
- Inherits:
-
Object
- Object
- Berkshelf::CookbookSource
- Extended by:
- Forwardable
- Defined in:
- lib/berkshelf/cookbook_source.rb
Overview
Constant Summary collapse
- @@valid_options =
[:constraint, :locations, :group, :locked_version]
- @@location_keys =
Hash.new
Instance Attribute Summary collapse
- #berksfile ⇒ Berkshelf::Berksfile readonly
- #cached_cookbook ⇒ Berkshelf::CachedCookbook
- #name ⇒ String readonly
- #version_constraint ⇒ Solve::Constraint readonly
Class Method Summary collapse
-
.add_location_key(location, klass) ⇒ Array<Symbol>
Register a location key with the CookbookSource class.
-
.add_valid_option(option) ⇒ Array<Symbol>
Add a option to the list of valid options.
-
.location_keys ⇒ Array<Symbol>
Returns an array of the registered source location keys.
-
.valid_options ⇒ Array<Symbol>
Returns an array of valid options to pass to the initializer.
- .validate_options(options) ⇒ Object
Instance Method Summary collapse
- #add_group(*local_groups) ⇒ Object
-
#cached_and_location(options = {}) ⇒ Array<CachedCookbook, Location>
Determine the CachedCookbook and Location information from the given options.
-
#downloaded? ⇒ Boolean
Returns true if the cookbook source has already been downloaded.
-
#groups ⇒ Array<Symbol>
The list of groups this CookbookSource belongs to.
-
#has_group?(group) ⇒ Boolean
Returns true if this CookbookSource has the given group.
-
#initialize(berksfile, name, options = {}) ⇒ CookbookSource
constructor
A new instance of CookbookSource.
-
#location ⇒ Berkshelf::Location
The location for this CookbookSource, such as a remote Chef Server, the community API, :git, or a :path location.
-
#locked_version ⇒ Solve::Version?
Get the locked version of this cookbook.
- #to_hash ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(berksfile, name, options = {}) ⇒ CookbookSource
Returns a new instance of CookbookSource.
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, = {}) self.class.() @berksfile = berksfile @name = name @locked_version = Solve::Version.new([:locked_version]) if [:locked_version] @version_constraint = Solve::Constraint.new([:locked_version] || [:constraint] || ">= 0.0.0") @cached_cookbook, @location = cached_and_location() add_group([:group]) if [:group] add_group(:default) if groups.empty? end |
Instance Attribute Details
#berksfile ⇒ Berkshelf::Berksfile (readonly)
72 73 74 |
# File 'lib/berkshelf/cookbook_source.rb', line 72 def berksfile @berksfile end |
#cached_cookbook ⇒ Berkshelf::CachedCookbook
78 79 80 |
# File 'lib/berkshelf/cookbook_source.rb', line 78 def cached_cookbook @cached_cookbook end |
#name ⇒ String (readonly)
74 75 76 |
# File 'lib/berkshelf/cookbook_source.rb', line 74 def name @name end |
#version_constraint ⇒ Solve::Constraint (readonly)
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
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
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_keys ⇒ Array<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.
20 21 22 |
# File 'lib/berkshelf/cookbook_source.rb', line 20 def location_keys @@location_keys end |
.valid_options ⇒ Array<Symbol>
Returns an array of valid options to pass to the initializer
11 12 13 |
# File 'lib/berkshelf/cookbook_source.rb', line 11 def @@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 () = (.keys - ) unless .empty? .collect! { |opt| "'#{opt}'" } raise InternalError, "Invalid options for Cookbook Source: #{.join(', ')}." end if (.keys & [:site, :path, :git]).size > 1 invalid = (.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.
128 129 130 |
# File 'lib/berkshelf/cookbook_source.rb', line 128 def cached_and_location( = {}) from_path() || from_cache() || from_default() end |
#downloaded? ⇒ Boolean
Returns true if the cookbook source has already been downloaded. A cookbook source is downloaded when a cached cookbook is present.
136 137 138 |
# File 'lib/berkshelf/cookbook_source.rb', line 136 def downloaded? !self.cached_cookbook.nil? end |
#groups ⇒ Array<Symbol>
The list of groups this CookbookSource belongs to.
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.
143 144 145 |
# File 'lib/berkshelf/cookbook_source.rb', line 143 def has_group?(group) groups.include?(group.to_sym) end |
#location ⇒ Berkshelf::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.
164 165 166 |
# File 'lib/berkshelf/cookbook_source.rb', line 164 def location @location end |
#locked_version ⇒ Solve::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.
155 156 157 |
# File 'lib/berkshelf/cookbook_source.rb', line 155 def locked_version @locked_version ||= cached_cookbook.try(:version) end |
#to_hash ⇒ Object
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_json ⇒ Object
189 190 191 |
# File 'lib/berkshelf/cookbook_source.rb', line 189 def to_json MultiJson.dump(self.to_hash, pretty: true) end |
#to_s ⇒ Object
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 |