Class: MrMurano::Setting

Inherits:
Object
  • Object
show all
Includes:
Verbose
Defined in:
lib/MrMurano/Setting.rb

Constant Summary collapse

SERVICE_MAP =
{
  'Device2' => 'Gateway',
}.freeze

Constants included from Verbose

Verbose::TABULARIZE_DATA_FORMAT_ERROR

Instance Method Summary collapse

Methods included from Verbose

ask_yes_no, #ask_yes_no, #assert, assert, cmd_confirm_delete!, #cmd_confirm_delete!, debug, #debug, dump_file_json, dump_file_plain, dump_file_yaml, #dump_output_file, #error, error, #error_file_format!, fancy_ticks, #fancy_ticks, #load_file_json, #load_file_plain, #load_file_yaml, #load_input_file, outf, #outf, #outformat_engine, #pluralize?, pluralize?, #prepare_hash_csv, #read_hashf!, #tabularize, tabularize, verbose, #verbose, warning, #warning, #whirly_interject, whirly_interject, #whirly_linger, whirly_linger, #whirly_msg, whirly_msg, #whirly_pause, whirly_pause, #whirly_start, whirly_start, #whirly_stop, whirly_stop, #whirly_unpause, whirly_unpause

Instance Method Details

#listObject

List all Settings classes and the accessors on them.

This is for letting users know which things can be read and written in the settings command.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/MrMurano/Setting.rb', line 71

def list
  result = {}
  ::MrMurano.constants.each do |maybe|
    begin
      gb = Object.const_get("MrMurano::#{maybe}::Settings")
      result[maybe] = gb.instance_methods(false).reject { |i| i.to_s[-1] == '=' }
    # rubocop:disable Lint/HandleExceptions: Do not suppress exceptions."
    rescue StandardError
      # EXPLAIN/2017-08-20: When/Why does this happen?
    end
  end
  result
  # MAYBE/2017-08-17:
  #   sort_by_name(result)
end

#mapservice(service) ⇒ String

Map service names into actual class names.

Some of the service names have changed over time and no longer match the class names that implement them. This maps them back, as well as correcting casing.

Parameters:

  • service (String)

    User facing service name

Returns:

  • (String)

    Internal class name for service



25
26
27
28
29
30
31
32
33
34
# File 'lib/MrMurano/Setting.rb', line 25

def mapservice(service)
  service = service.to_s.downcase
  SERVICE_MAP.each_pair do |k, v|
    return v if [k.downcase, v.downcase].include? service
  end
  # rubocop:disable Style/PerlBackrefs: "Avoid the use of Perl-style backrefs."
  # Because of the upcase, we cannot just call, e.g.,
  #   service.sub(/(.)(.*)/, "\\1\\2")
  service.sub(/(.)(.*)/) { "#{$1.upcase}#{$2.downcase}" }
end

#read(service, setting) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/MrMurano/Setting.rb', line 36

def read(service, setting)
  debug %(Looking up class "MrMurano::#{mapservice(service)}::Settings")
  gb = Object.const_get("MrMurano::#{mapservice(service)}::Settings").new
  meth = setting.to_sym
  debug %(Looking up method "#{meth}")
  return gb.__send__(meth) if gb.respond_to?(meth)
  error "Unknown setting '#{setting}' on '#{service}'"
rescue NameError => e
  error %(No Settings on "#{service}")
  if $cfg['tool.debug']
    error e.message
    error e.to_s
  end
end

#write(service, setting, value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/MrMurano/Setting.rb', line 51

def write(service, setting, value)
  debug %(Looking up class "MrMurano::#{mapservice(service)}::Settings")
  gb = Object.const_get("MrMurano::#{mapservice(service)}::Settings").new
  meth = "#{setting}=".to_sym
  debug %(Looking up method "#{meth}")
  return gb.__send__(meth, value) if gb.respond_to? meth
  error "Unknown setting '#{setting}' on '#{service}'"
rescue NameError => e
  error %(No Settings on "#{service}")
  if $cfg['tool.debug']
    error e.message
    error e.to_s
  end
end