Class: Stove::Cookbook::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/stove/cookbook/metadata.rb

Overview

Borrowed and modified from: https://raw.github.com/opscode/chef/11.4.0/lib/chef/cookbook/metadata.rb

Copyright

Copyright 2008-2010 Opscode, Inc.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Chef::Cookbook::Metadata

Chef::Cookbook::Metadata provides a convenient DSL for declaring metadata about Chef Cookbooks.

Constant Summary collapse

DEFAULT_VERSION =
'>= 0.0.0'.freeze
COMPARISON_FIELDS =
[
  :name, :description, :long_description, :maintainer,
  :maintainer_email, :license, :platforms, :dependencies,
  :recommendations, :suggestions, :conflicting, :providing,
  :replacing, :attributes, :groupings, :recipes, :version
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookbook = nil, maintainer = 'YOUR_COMPANY_NAME', maintainer_email = 'YOUR_EMAIL', license = 'none') ⇒ Metadata

Returns a new instance of Metadata.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/stove/cookbook/metadata.rb', line 106

def initialize(cookbook = nil, maintainer = 'YOUR_COMPANY_NAME', maintainer_email = 'YOUR_EMAIL', license = 'none')
  @cookbook         = cookbook
  @name             = cookbook ? cookbook.name : ''
  @long_description = ''
  @source_url       = Stove::Mash.new
  @issues_url       = Stove::Mash.new
  @platforms        = Stove::Mash.new
  @dependencies     = Stove::Mash.new
  @recommendations  = Stove::Mash.new
  @suggestions      = Stove::Mash.new
  @conflicting      = Stove::Mash.new
  @providing        = Stove::Mash.new
  @replacing        = Stove::Mash.new
  @attributes       = Stove::Mash.new
  @groupings        = Stove::Mash.new
  @recipes          = Stove::Mash.new

  self.maintainer(maintainer)
  self.maintainer_email(maintainer_email)
  self.license(license)
  self.description('A fabulous new cookbook')
  self.version('0.0.0')

  if cookbook
    @recipes = cookbook.fully_qualified_recipe_names.inject({}) do |r, e|
      e = self.name if e =~ /::default$/
      r[e] = ""
      self.provides e
      r
    end
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



101
102
103
# File 'lib/stove/cookbook/metadata.rb', line 101

def attributes
  @attributes
end

#conflictingObject (readonly)

Returns the value of attribute conflicting.



98
99
100
# File 'lib/stove/cookbook/metadata.rb', line 98

def conflicting
  @conflicting
end

#cookbookObject (readonly)

Returns the value of attribute cookbook.



93
94
95
# File 'lib/stove/cookbook/metadata.rb', line 93

def cookbook
  @cookbook
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



95
96
97
# File 'lib/stove/cookbook/metadata.rb', line 95

def dependencies
  @dependencies
end

#groupingsObject (readonly)

Returns the value of attribute groupings.



102
103
104
# File 'lib/stove/cookbook/metadata.rb', line 102

def groupings
  @groupings
end

#platformsObject (readonly)

Returns the value of attribute platforms.



94
95
96
# File 'lib/stove/cookbook/metadata.rb', line 94

def platforms
  @platforms
end

#providingObject (readonly)

Returns the value of attribute providing.



99
100
101
# File 'lib/stove/cookbook/metadata.rb', line 99

def providing
  @providing
end

#recipesObject (readonly)

Returns the value of attribute recipes.



103
104
105
# File 'lib/stove/cookbook/metadata.rb', line 103

def recipes
  @recipes
end

#recommendationsObject (readonly)

Returns the value of attribute recommendations.



96
97
98
# File 'lib/stove/cookbook/metadata.rb', line 96

def recommendations
  @recommendations
end

#replacingObject (readonly)

Returns the value of attribute replacing.



100
101
102
# File 'lib/stove/cookbook/metadata.rb', line 100

def replacing
  @replacing
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



97
98
99
# File 'lib/stove/cookbook/metadata.rb', line 97

def suggestions
  @suggestions
end

#version(arg = UNSET_VALUE) ⇒ Object (readonly)

Returns the value of attribute version.



104
105
106
# File 'lib/stove/cookbook/metadata.rb', line 104

def version
  @version
end

Class Method Details

.def_attribute(field) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/stove/cookbook/metadata.rb', line 31

def def_attribute(field)
  class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{field}(arg = nil)
      set_or_return(:#{field}, arg)
    end
  EOM
end

.def_meta_cookbook(field, instance_variable) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/stove/cookbook/metadata.rb', line 39

def def_meta_cookbook(field, instance_variable)
  class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{field}(thing, *args)
      version = args.first
      @#{instance_variable}[thing] = version || DEFAULT_VERSION
      @#{instance_variable}[thing]
    end
  EOM
end

.def_meta_setter(field, instance_variable) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/stove/cookbook/metadata.rb', line 49

def def_meta_setter(field, instance_variable)
  class_eval <<-EOM, __FILE__, __LINE__ + 1
    def #{field}(name, description)
      @#{instance_variable}[name] = description
      @#{instance_variable}
    end
  EOM
end

.from_file(path) ⇒ Object



27
28
29
# File 'lib/stove/cookbook/metadata.rb', line 27

def from_file(path)
  new.from_file(path)
end

Instance Method Details

#==(other) ⇒ Object



150
151
152
153
154
# File 'lib/stove/cookbook/metadata.rb', line 150

def ==(other)
  COMPARISON_FIELDS.inject(true) do |equal_so_far, field|
    equal_so_far && other.respond_to?(field) && (other.send(field) == send(field))
  end
end

#from_file(path) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'lib/stove/cookbook/metadata.rb', line 139

def from_file(path)
  path = path.to_s

  if File.exist?(path) && File.readable?(path)
    self.instance_eval(IO.read(path), path, 1)
    self
  else
    raise Error::MetadataNotFound.new(path: path)
  end
end

#to_hash(extended_metadata = false) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/stove/cookbook/metadata.rb', line 164

def to_hash( = false)
  hash = {
    'name'             => self.name,
    'version'          => self.version,
    'description'      => self.description,
    'long_description' => self.long_description,
    'maintainer'       => self.maintainer,
    'maintainer_email' => self.maintainer_email,
    'license'          => self.license,
    'platforms'        => self.platforms,
    'dependencies'     => self.dependencies,
    'recommendations'  => self.recommendations,
    'suggestions'      => self.suggestions,
    'conflicting'      => self.conflicting,
    'providing'        => self.providing,
    'replacing'        => self.replacing,
    'attributes'       => self.attributes,
    'groupings'        => self.groupings,
    'recipes'          => self.recipes,
  }

  if 
    hash['source_url'] = self.source_url
    hash['issues_url'] = self.issues_url
  end

  return hash
end