Class: PuppetX::Rustup::Property::Subresources

Inherits:
Set
  • Object
show all
Defined in:
lib/puppet_x/rustup/property/subresources.rb

Overview

A list of subresources

This is a list of hashes that correspond to sub-resources. It expects each hash to have, at the least, a ‘title’ entry that identifies it uniquely within this list of resources, and an ‘ensure’ entry that contains either ‘absent’ or something else that will be interpreted as not-absent.

Instance Method Summary collapse

Instance Method Details

#entry_identity(entry) ⇒ Object

Get the identity of the entry.

This is akin to the title of a resource. It should uniquely identify this entry within in the set. If two entries have the same identity, then it is an error.



17
18
19
# File 'lib/puppet_x/rustup/property/subresources.rb', line 17

def entry_identity(entry)
  entry.reject { |k| k == 'ensure' }.hash
end

#is_to_s(values) ⇒ String

Format existing values for display.

Often we use a subresource class to handle subresource collections in the provider, which interferes with the display of changed values.

rubocop:disable Naming/PredicateName

Parameters:

  • values (Array)

    the values to format as a string

Returns:

  • (String)

    a pretty printing string



110
111
112
113
114
115
# File 'lib/puppet_x/rustup/property/subresources.rb', line 110

def is_to_s(values)
  if values.is_a? PuppetX::Rustup::Provider::Collection
    values = values.system
  end
  super
end

#normalize_is_entry(entry) ⇒ Object

Do any normalization required for an entry in ‘is`.

Return the normalized entry or ‘nil` to skip it. If an entry has the equivalent of `ensure => absent`, this should return `nil`.

This clones entry and passes it to ‘normalize_is_entry!`. Unless you need to return `nil`, you should override that function.



49
50
51
52
53
# File 'lib/puppet_x/rustup/property/subresources.rb', line 49

def normalize_is_entry(entry)
  cloned = entry.clone
  normalize_is_entry!(cloned)
  cloned
end

#normalize_is_entry!(_entry) ⇒ Object

Do any normalization required for an entry in ‘is`.

This should modify the parameter.



63
# File 'lib/puppet_x/rustup/property/subresources.rb', line 63

def normalize_is_entry!(_entry); end

#normalize_should_entry(entry) ⇒ Object

Do any normalization required for an entry in ‘should`.

Return the normalized entry or ‘nil` to skip it.

This clones entry and passes it to ‘normalize_should_entry!`. Unless you need to return `nil`, you should override that function.



36
37
38
39
40
# File 'lib/puppet_x/rustup/property/subresources.rb', line 36

def normalize_should_entry(entry)
  cloned = entry.clone
  normalize_should_entry!(cloned)
  cloned
end

#normalize_should_entry!(_entry) ⇒ Object

Do any normalization required for an entry in ‘should`.

This should modify the parameter.



58
# File 'lib/puppet_x/rustup/property/subresources.rb', line 58

def normalize_should_entry!(_entry); end

#should_entry_absent?(entry) ⇒ Boolean

Does this entry in ‘should` have the equivalent of `ensure => absent`?

This is used to determine if there is a change. If this returns ‘true` and there is no corresponding entry in `is`, then it will not be considered a change.

Returns:

  • (Boolean)


26
27
28
# File 'lib/puppet_x/rustup/property/subresources.rb', line 26

def should_entry_absent?(entry)
  entry['ensure'] == 'absent'
end

#validate_in(entry, attr, valid_set) ⇒ Object

Raise a friendly error if a hash entry is not in valid_set.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/puppet_x/rustup/property/subresources.rb', line 66

def validate_in(entry, attr, valid_set)
  unless valid_set.any? entry[attr]
    raise Puppet::Error, 'Expected %{name} Hash entry %{attr} to be one of ' \
      '%{valid}, got %{value}' % {
        name: name,
        attr: attr.inspect,
        valid: valid_set.inspect,
        value: entry[attr].inspect,
      }
  end
end

#validate_nil_or_non_empty_string(entry, attr) ⇒ Object

Raise a friendly error if a hash entry is not nil or a non-empty string.



91
92
93
94
95
96
97
98
99
100
# File 'lib/puppet_x/rustup/property/subresources.rb', line 91

def validate_nil_or_non_empty_string(entry, attr)
  unless PuppetX::Rustup::Util.nil_or_non_empty_string? entry[attr]
    raise Puppet::Error, 'Expected %{name} Hash entry %{attr} to be nil ' \
      'or a non-empty string, got %{value}' % {
        name: name,
        attr: attr.inspect,
        value: entry[attr].inspect,
      }
  end
end

#validate_non_empty_string(entry, attr) ⇒ Object

Raise a friendly error if a hash entry is not a non-empty string.



79
80
81
82
83
84
85
86
87
88
# File 'lib/puppet_x/rustup/property/subresources.rb', line 79

def validate_non_empty_string(entry, attr)
  unless PuppetX::Rustup::Util.non_empty_string? entry[attr]
    raise Puppet::Error, 'Expected %{name} Hash entry %{attr} to be a ' \
      'non-empty string, got %{value}' % {
        name: name,
        attr: attr.inspect,
        value: entry[attr].inspect,
      }
  end
end