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.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/stove/cookbook/metadata.rb', line 100

def initialize(cookbook = nil, maintainer = 'YOUR_COMPANY_NAME', maintainer_email = 'YOUR_EMAIL', license = 'none')
  @cookbook         = cookbook
  @name             = cookbook ? cookbook.name : ''
  @long_description = ''
  @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.



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

def attributes
  @attributes
end

#conflictingObject (readonly)

Returns the value of attribute conflicting.



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

def conflicting
  @conflicting
end

#cookbookObject (readonly)

Returns the value of attribute cookbook.



87
88
89
# File 'lib/stove/cookbook/metadata.rb', line 87

def cookbook
  @cookbook
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



89
90
91
# File 'lib/stove/cookbook/metadata.rb', line 89

def dependencies
  @dependencies
end

#groupingsObject (readonly)

Returns the value of attribute groupings.



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

def groupings
  @groupings
end

#platformsObject (readonly)

Returns the value of attribute platforms.



88
89
90
# File 'lib/stove/cookbook/metadata.rb', line 88

def platforms
  @platforms
end

#providingObject (readonly)

Returns the value of attribute providing.



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

def providing
  @providing
end

#recipesObject (readonly)

Returns the value of attribute recipes.



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

def recipes
  @recipes
end

#recommendationsObject (readonly)

Returns the value of attribute recommendations.



90
91
92
# File 'lib/stove/cookbook/metadata.rb', line 90

def recommendations
  @recommendations
end

#replacingObject (readonly)

Returns the value of attribute replacing.



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

def replacing
  @replacing
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



91
92
93
# File 'lib/stove/cookbook/metadata.rb', line 91

def suggestions
  @suggestions
end

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

Returns the value of attribute version.



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

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



142
143
144
145
146
# File 'lib/stove/cookbook/metadata.rb', line 142

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



131
132
133
134
135
136
137
138
139
140
# File 'lib/stove/cookbook/metadata.rb', line 131

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_hashObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/stove/cookbook/metadata.rb', line 156

def to_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,
  }
end

#to_json(*args) ⇒ Object



178
179
180
# File 'lib/stove/cookbook/metadata.rb', line 178

def to_json(*args)
  JSON.pretty_generate(self.to_hash)
end