Class: Bolt::Puppetfile::Module

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/puppetfile/module.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, name, version = nil) ⇒ Module

Returns a new instance of Module.



13
14
15
16
17
18
19
20
# File 'lib/bolt/puppetfile/module.rb', line 13

def initialize(owner, name, version = nil)
  @owner   = owner
  @name    = name

  if version.is_a?(String)
    @version = version[0] == '=' ? version[1..-1] : version
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/bolt/puppetfile/module.rb', line 11

def name
  @name
end

#ownerObject (readonly)

Returns the value of attribute owner.



11
12
13
# File 'lib/bolt/puppetfile/module.rb', line 11

def owner
  @owner
end

#versionObject (readonly)

Returns the value of attribute version.



11
12
13
# File 'lib/bolt/puppetfile/module.rb', line 11

def version
  @version
end

Class Method Details

.from_hash(mod) ⇒ Object

Creates a new module from a hash.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bolt/puppetfile/module.rb', line 24

def self.from_hash(mod)
  unless mod['name'].is_a?(String)
    raise Bolt::ValidationError,
          "Module name must be a String, not #{mod['name'].inspect}"
  end

  owner, name = mod['name'].tr('/', '-').split('-', 2)

  unless owner && name
    raise Bolt::ValidationError, "Module name #{mod['name']} must include both the owner and module name."
  end

  new(owner, name, mod['version_requirement'])
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Checks two modules for equality.

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/bolt/puppetfile/module.rb', line 48

def eql?(other)
  self.class == other.class &&
    @owner == other.owner &&
    @name == other.name &&
    versions_intersect?(other)
end

#hashObject

Hashes the module.



68
69
70
# File 'lib/bolt/puppetfile/module.rb', line 68

def hash
  [@owner, @name].hash
end

#titleObject Also known as: to_s

Returns the module’s title.



41
42
43
# File 'lib/bolt/puppetfile/module.rb', line 41

def title
  "#{@owner}-#{@name}"
end

#to_hashObject

Returns a hash representation similar to the module declaration.



75
76
77
78
79
80
# File 'lib/bolt/puppetfile/module.rb', line 75

def to_hash
  {
    'name'                => title,
    'version_requirement' => version
  }.compact
end

#to_specObject

Returns the Puppetfile specification for the module.



84
85
86
87
88
89
90
# File 'lib/bolt/puppetfile/module.rb', line 84

def to_spec
  if @version
    "mod #{title.inspect}, #{@version.inspect}"
  else
    "mod #{title.inspect}"
  end
end

#versions_intersect?(other) ⇒ Boolean

Returns true if the versions of two modules intersect. Used to determine if an installed module satisfies the version requirement of another.

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'lib/bolt/puppetfile/module.rb', line 59

def versions_intersect?(other)
  range       = ::SemanticPuppet::VersionRange.parse(@version || '')
  other_range = ::SemanticPuppet::VersionRange.parse(other.version || '')

  range.intersection(other_range) != ::SemanticPuppet::VersionRange::EMPTY_RANGE
end