Class: Berkshelf::CookbookSource

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

Overview

Author:

Constant Summary collapse

@@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(name, options = {}) ⇒ CookbookSource

@param [String] name

@param [Hash] options

@option options [String, Solve::Constraint] constraint
  version constraint to resolve for this source
@option options [String] :git
  the Git URL to clone
@option options [String] :site
  a URL pointing to a community API endpoint
@option options [String] :path
  a filepath to the cookbook on your local disk
@option options [Symbol, Array] :group
  the group or groups that the cookbook belongs to
@option options [String] :ref
  the commit hash or an alias to a commit hash to clone
@option options [String] :branch
  same as ref
@option options [String] :tag
  same as tag
@option options [String] :locked_version


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/berkshelf/cookbook_source.rb', line 94

def initialize(name, options = {})
  @name = name
  @version_constraint = Solve::Constraint.new(options[:constraint] || ">= 0.0.0")
  @groups = []
  @cached_cookbook = nil
  @location = nil

  self.class.validate_options(options)

  unless (options.keys & self.class.location_keys.keys).empty?
    @location = Location.init(name, version_constraint, options)
  end

  if @location.is_a?(PathLocation)
    @cached_cookbook = CachedCookbook.from_path(location.path)
  end

  @locked_version = Solve::Version.new(options[:locked_version]) if options[:locked_version]

  add_group(options[:group]) if options[:group]
  add_group(:default) if groups.empty?
end

Instance Attribute Details

#cached_cookbookObject

Returns the value of attribute cached_cookbook.



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

def cached_cookbook
  @cached_cookbook
end

#groupsObject (readonly)

Returns the value of attribute groups.



68
69
70
# File 'lib/berkshelf/cookbook_source.rb', line 68

def groups
  @groups
end

#locationObject (readonly)

Returns the value of attribute location.



69
70
71
# File 'lib/berkshelf/cookbook_source.rb', line 69

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



66
67
68
# File 'lib/berkshelf/cookbook_source.rb', line 66

def name
  @name
end

#version_constraintObject (readonly)

Returns the value of attribute version_constraint.



67
68
69
# File 'lib/berkshelf/cookbook_source.rb', line 67

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


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

Parameters:

  • option (Symbol)

Returns:

  • (Array<Symbol>)

See Also:

  • #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_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>)


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

def location_keys
  @@location_keys
end

.valid_optionsArray<Symbol>

Returns an array of valid options to pass to the initializer

Returns:

  • (Array<Symbol>)


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

def valid_options
  @@valid_options
end

.validate_options(options) ⇒ Object



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

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

  true
end

Instance Method Details

#add_group(*groups) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/berkshelf/cookbook_source.rb', line 117

def add_group(*groups)
  groups = groups.first if groups.first.is_a?(Array)
  groups.each do |group|
    group = group.to_sym
    @groups << group unless @groups.include?(group)
  end
end

#downloaded?Boolean

Returns true if the cookbook source has already been downloaded. A cookbook source is downloaded when a cached cookbooked is present.

Returns:

  • (Boolean)


129
130
131
# File 'lib/berkshelf/cookbook_source.rb', line 129

def downloaded?
  !self.cached_cookbook.nil?
end

#has_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#to_hashObject



143
144
145
146
147
148
149
# File 'lib/berkshelf/cookbook_source.rb', line 143

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_jsonObject



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

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

#to_sObject



137
138
139
140
141
# File 'lib/berkshelf/cookbook_source.rb', line 137

def to_s
  msg = "#{self.name} (#{self.version_constraint}) groups: #{self.groups}"
  msg << " location: #{self.location}" if self.location
  msg
end