Class: Berkshelf::CookbookSource
- Inherits:
-
Object
- Object
- Berkshelf::CookbookSource
- 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
- #berksfile ⇒ Berkshelf::Berksfile readonly
- #cached_cookbook ⇒ Berkshelf::CachedCookbook
-
#groups ⇒ Array<Symbol>
readonly
The list of groups this CookbookSource belongs to.
-
#location ⇒ Berkshelf::Location
readonly
The location for this CookbookSource, such as a remote Chef Server, the community API, :git, or a :path location.
- #name ⇒ String readonly
- #version_constraint ⇒ Solve::Constraint
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.
-
#has_group?(group) ⇒ Boolean
Returns true if this CookbookSource has the given group.
-
#initialize(berksfile, name, options = {}) ⇒ CookbookSource
constructor
A new instance of CookbookSource.
- #inspect ⇒ Object
-
#locked_version ⇒ Solve::Version?
Get the locked version of this cookbook.
- #to_hash ⇒ Object
- #to_json(options = {}) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(berksfile, name, options = {}) ⇒ CookbookSource
Returns a new instance of CookbookSource.
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 = self.class.() @berksfile = berksfile @name = name @locked_version = Solve::Version.new([:locked_version]) if [:locked_version] @version_constraint = Solve::Constraint.new([:constraint] || DEFAULT_CONSTRAINT) @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)
71 72 73 |
# File 'lib/berkshelf/cookbook_source.rb', line 71 def berksfile @berksfile end |
#cached_cookbook ⇒ Berkshelf::CachedCookbook
81 82 83 |
# File 'lib/berkshelf/cookbook_source.rb', line 81 def cached_cookbook @cached_cookbook end |
#groups ⇒ Array<Symbol> (readonly)
The list of groups this CookbookSource belongs to.
75 76 77 |
# File 'lib/berkshelf/cookbook_source.rb', line 75 def groups @groups end |
#location ⇒ Berkshelf::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.
77 78 79 |
# File 'lib/berkshelf/cookbook_source.rb', line 77 def location @location end |
#name ⇒ String (readonly)
73 74 75 |
# File 'lib/berkshelf/cookbook_source.rb', line 73 def name @name end |
#version_constraint ⇒ 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
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
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_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.
19 20 21 |
# File 'lib/berkshelf/cookbook_source.rb', line 19 def location_keys @@location_keys end |
.valid_options ⇒ Array<Symbol>
Returns an array of valid options to pass to the initializer
10 11 12 |
# File 'lib/berkshelf/cookbook_source.rb', line 10 def @@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 () = (.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
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.
135 136 137 |
# File 'lib/berkshelf/cookbook_source.rb', line 135 def cached_and_location( = {}) from_path() || 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.
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.
150 151 152 |
# File 'lib/berkshelf/cookbook_source.rb', line 150 def has_group?(group) groups.include?(group.to_sym) end |
#inspect ⇒ Object
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_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.
162 163 164 |
# File 'lib/berkshelf/cookbook_source.rb', line 162 def locked_version @locked_version ||= cached_cookbook.try(:version) end |
#to_hash ⇒ Object
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( = {}) JSON.pretty_generate(to_hash, ) end |
#to_s ⇒ Object
182 183 184 |
# File 'lib/berkshelf/cookbook_source.rb', line 182 def to_s "#<Berkshelf::CookbookSource: #{name} (#{version_constraint})>" end |