class Puppet::ModuleTool::Metadata

This class provides a data structure representing a module’s metadata. @api private

Constants

DEFAULTS

Attributes

module_name[RW]
name[RW]

Public Instance Methods

add_dependency(name, version_requirement=nil, repository=nil) click to toggle source

Validates the name and version_requirement for a dependency, then creates the Dependency and adds it. Returns the Dependency that was added.

# File lib/puppet/module_tool/metadata.rb, line 64
def add_dependency(name, version_requirement=nil, repository=nil)
  validate_name(name)
  validate_version_range(version_requirement) if version_requirement

  if dup = @data['dependencies'].find { |d| d.full_module_name == name && d.version_requirement != version_requirement }
    raise ArgumentError, "Dependency conflict for #{full_module_name}: Dependency #{name} was given conflicting version requirements #{version_requirement} and #{dup.version_requirement}. Verify that there are no duplicates in the metadata.json or the Modulefile."
  end

  dep = Dependency.new(name, version_requirement, repository)
  @data['dependencies'].add(dep)

  dep
end
dashed_name() click to toggle source

Returns a filesystem-friendly version of this module name.

# File lib/puppet/module_tool/metadata.rb, line 35
def dashed_name
  @data['name'].tr('/', '-') if @data['name']
end
Also aliased as: full_module_name
dependencies() click to toggle source
# File lib/puppet/module_tool/metadata.rb, line 86
def dependencies
  @data['dependencies'].to_a
end
description() click to toggle source

Provides an accessor for the now defunct ‘description’ property. This addresses a regression in Puppet 3.6.x where previously valid templates refering to the ‘description’ property were broken. @deprecated

# File lib/puppet/module_tool/metadata.rb, line 82
def description
  @data['description']
end
full_module_name() click to toggle source
Alias for: dashed_name
method_missing(name, *args) click to toggle source

Expose any metadata keys as callable reader methods.

# File lib/puppet/module_tool/metadata.rb, line 122
def method_missing(name, *args)
  return @data[name.to_s] if @data.key? name.to_s
  super
end
release_name() click to toggle source

Returns a string that uniquely represents this version of this module.

# File lib/puppet/module_tool/metadata.rb, line 40
def release_name
  return nil unless @data['name'] && @data['version']
  [ dashed_name, @data['version'] ].join('-')
end
to_data_hash() click to toggle source
Alias for: to_hash
to_hash() click to toggle source

Returns a hash of the module’s metadata. Used by Puppet’s automated serialization routines.

@see Puppet::Network::FormatSupport#to_data_hash

# File lib/puppet/module_tool/metadata.rb, line 94
def to_hash
  @data
end
Also aliased as: to_data_hash
to_json() click to toggle source
# File lib/puppet/module_tool/metadata.rb, line 99
def to_json
  data = @data.dup.merge('dependencies' => dependencies)

  # This is used to simulate an ordered hash.  In particular, some keys
  # are promoted to the top of the serialized hash (while others are
  # demoted) for human-friendliness.
  #
  # This particularly works around the lack of ordered hashes in 1.8.7.
  promoted_keys = %w[ name version author summary license source ]
  demoted_keys = %w[ dependencies ]
  keys = data.keys
  keys -= promoted_keys
  keys -= demoted_keys

  contents = (promoted_keys + keys + demoted_keys).map do |k|
    value = (JSON.pretty_generate(data[k]) rescue data[k].to_json)
    "#{k.to_json}: #{value}"
  end

  "{\n" + contents.join(",\n").gsub(/^/, '  ') + "\n}\n"
end
update(data) click to toggle source

Merges the current set of metadata with another metadata hash. This method also handles the validation of module names and versions, in an effort to be proactive about module publishing constraints.

# File lib/puppet/module_tool/metadata.rb, line 51
def update(data)
  process_name(data) if data['name']
  process_version(data) if data['version']
  process_source(data) if data['source']
  merge_dependencies(data) if data['dependencies']

  @data.merge!(data)
  return self
end

Public Class Methods

new() click to toggle source
# File lib/puppet/module_tool/metadata.rb, line 29
def initialize
  @data = DEFAULTS.dup
  @data['dependencies'] = @data['dependencies'].dup
end