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

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.



99
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
# File 'lib/stove/cookbook/metadata.rb', line 99

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.



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

def attributes
  @attributes
end

#conflictingObject (readonly)

Returns the value of attribute conflicting.



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

def conflicting
  @conflicting
end

#cookbookObject (readonly)

Returns the value of attribute cookbook.



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

def cookbook
  @cookbook
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



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

def dependencies
  @dependencies
end

#groupingsObject (readonly)

Returns the value of attribute groupings.



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

def groupings
  @groupings
end

#platformsObject (readonly)

Returns the value of attribute platforms.



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

def platforms
  @platforms
end

#providingObject (readonly)

Returns the value of attribute providing.



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

def providing
  @providing
end

#recipesObject (readonly)

Returns the value of attribute recipes.



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

def recipes
  @recipes
end

#recommendationsObject (readonly)

Returns the value of attribute recommendations.



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

def recommendations
  @recommendations
end

#replacingObject (readonly)

Returns the value of attribute replacing.



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

def replacing
  @replacing
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



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

def suggestions
  @suggestions
end

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

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.def_attribute(field) ⇒ Object



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

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



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

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

.def_meta_setter(field, instance_variable) ⇒ Object



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

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



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

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

Instance Method Details

#==(other) ⇒ Object



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

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



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

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 Stove::MetadataNotFound.new(path)
  end
end

#to_hashObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/stove/cookbook/metadata.rb', line 152

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



174
175
176
# File 'lib/stove/cookbook/metadata.rb', line 174

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