Module: PuppetX::EnterpriseModules::ExtendHash

Defined in:
lib/puppet_x/enterprisemodules/extend_hash.rb

Overview

rubocop:disable Style/Documentation

Instance Method Summary collapse

Instance Method Details

#content_if(key, string = default_value_for_key(key)) ⇒ Object



26
27
28
# File 'lib/puppet_x/enterprisemodules/extend_hash.rb', line 26

def content_if(key, string = default_value_for_key(key))
  "#{string} #{value_for(key)}" if exists?(key)
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/puppet_x/enterprisemodules/extend_hash.rb', line 34

def exists?(key)
  case value_for(key)
  when nil, '' then false
  else
    true
  end
end

#included(parent) ⇒ Object

rubocop:enable Style/Documentation



11
12
13
# File 'lib/puppet_x/enterprisemodules/extend_hash.rb', line 11

def included(parent)
  parent.extend(self)
end

#key_if(key, string = default_value_for_key(key)) ⇒ Object



30
31
32
# File 'lib/puppet_x/enterprisemodules/extend_hash.rb', line 30

def key_if(key, string = default_value_for_key(key))
  string.to_s if exists?(key)
end

#use_hash(content) ⇒ Object



22
23
24
# File 'lib/puppet_x/enterprisemodules/extend_hash.rb', line 22

def use_hash(content)
  @__content = content # use a double underscore to make sure it doesn't interfere
end

#value_for(key) ⇒ Object

rubocop:disable Lint/ShadowingOuterLocalVariable



43
44
45
46
47
48
# File 'lib/puppet_x/enterprisemodules/extend_hash.rb', line 43

def value_for(key)
  keys = key.split('.')
  keys.inject(@__content) do |location, key|
    location && location[key] ? location[key] : nil
  end
end

#with_hash(content) {|proc| ... } ⇒ Object

Yields:

  • (proc)


15
16
17
18
19
20
# File 'lib/puppet_x/enterprisemodules/extend_hash.rb', line 15

def with_hash(content, &proc)
  old_content = @__content
  @__content = content # use a double underscore to make sure it doesn't interfere
  yield proc
  @__content = old_content
end