Class: RegistryKey

Inherits:
Object
  • Object
show all
Defined in:
lib/resources/registry_key.rb

Overview

Usage: describe registry_key(‘Task Scheduler’,‘HKEY_LOCAL_MACHINESYSTEMCurrentControlSetservicesSchedule’) do

its('Start') { should eq 2 }

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, reg_key = nil) ⇒ RegistryKey

Returns a new instance of RegistryKey.



18
19
20
21
22
23
# File 'lib/resources/registry_key.rb', line 18

def initialize(name, reg_key = nil)
  # if we have one parameter, we use it as name
  reg_key ||= name
  @name = name
  @reg_key = reg_key
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth) ⇒ Object

returns nil, if not existant or value



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/resources/registry_key.rb', line 39

def method_missing(meth)
  # get data
  val = registry_value(@reg_key, meth)

  # verify data
  if (val[:exit_code] == 0)
    return convert_value(val[:data])
  else
    return nil
  end
end

Instance Attribute Details

#reg_keyObject

Returns the value of attribute reg_key.



16
17
18
# File 'lib/resources/registry_key.rb', line 16

def reg_key
  @reg_key
end

Instance Method Details

#convert_value(value) ⇒ Object



32
33
34
35
36
# File 'lib/resources/registry_key.rb', line 32

def convert_value(value)
  val = value.strip
  val = val.to_i if val.match(/^\d+$/)
  val
end

#registry_value(path, key) ⇒ Object



25
26
27
28
29
30
# File 'lib/resources/registry_key.rb', line 25

def registry_value(path, key)
  cmd = "(Get-Item 'Registry::#{path}').GetValue('#{key}')"
  command_result ||= inspec.command(cmd)
  val = { exit_code: command_result.exit_status.to_i, data: command_result.stdout }
  val
end

#to_sObject



51
52
53
# File 'lib/resources/registry_key.rb', line 51

def to_s
  "Registry Key #{@name}"
end