Module: RMonitor::ProfileHelpers

Included in:
Profiles
Defined in:
lib/rmonitor/helpers/profile_helpers.rb

Instance Method Summary collapse

Instance Method Details

#best_matching_configuration(device, mode, rate) ⇒ Object



37
38
39
40
41
42
# File 'lib/rmonitor/helpers/profile_helpers.rb', line 37

def best_matching_configuration(device, mode, rate)
  device[:configurations].find do |configuration|
    (!mode or configuration[:mode] == mode) &&
        (!rate or configuration[:rate] == rate)
  end
end

#has_matching_configuration(device, mode, rate) ⇒ Object



44
45
46
# File 'lib/rmonitor/helpers/profile_helpers.rb', line 44

def has_matching_configuration(device, mode, rate)
  !best_matching_configuration(device, mode, rate).nil?
end

#invokable?(devices, profile, verbose = false) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/rmonitor/helpers/profile_helpers.rb', line 3

def invokable?(devices, profile, verbose = false)
  necessary_devices_present    = necessary_devices_present?(devices, profile)
  user_defined_rules_satisfied = user_defined_rules_satisfied?(profile)

  if necessary_devices_present && !user_defined_rules_satisfied && verbose
    method = (profile[:options][:not_if] || profile[:options][:only_if]).name

    puts "#{profile[:name].inspect} deemed not invokable due to #{method.inspect}."
  end

  necessary_devices_present && user_defined_rules_satisfied
end

#necessary_devices_present?(devices, profile) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
# File 'lib/rmonitor/helpers/profile_helpers.rb', line 16

def necessary_devices_present?(devices, profile)
  profile[:devices].all? do |wanted_device|
    device = devices.find { |d| d[:name] == wanted_device[:name] }

    device and
        has_matching_configuration(device,
                                   wanted_device[:mode],
                                   wanted_device[:rate])
  end
end

#user_defined_rules_satisfied?(profile) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
# File 'lib/rmonitor/helpers/profile_helpers.rb', line 27

def user_defined_rules_satisfied?(profile)
  if profile[:options][:only_if]
    profile[:options][:only_if].call
  elsif profile[:options][:not_if]
    !profile[:options][:not_if].call
  else
    true
  end
end