Class: Zabbirc::Setting

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbirc/setting.rb

Constant Summary collapse

DEFAULTS =
ActiveSupport::HashWithIndifferentAccess.new({
    notify: true,
    notify_recoveries: true,
    primary_channel: nil,
    events_priority: Zabbirc.config.default_events_priority,
    host_groups: ActiveSupport::HashWithIndifferentAccess.new
})

Instance Method Summary collapse

Constructor Details

#initializeSetting

Returns a new instance of Setting.



12
13
14
# File 'lib/zabbirc/setting.rb', line 12

def initialize
  @options = ActiveSupport::HashWithIndifferentAccess.new DEFAULTS.deep_dup
end

Instance Method Details

#fetch(name, value, *options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zabbirc/setting.rb', line 41

def fetch name, value, *options
  host_group_id = parse_host_group_id(options)

  if host_group_id
    if host_group_options(host_group_id)[name].nil?
      set_with_host_group name, value, host_group_id
    else
      get name, host_group_id: host_group_id
    end
  else
    @options[name] ||= value
  end
end

#get(name, *options) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/zabbirc/setting.rb', line 32

def get name, *options
  host_group_id = parse_host_group_id(options)
  if host_group_id
    first_not_nil(host_group_options(host_group_id)[name], @options[name])
  else
    @options[name]
  end
end

#restore(stored_options) ⇒ Object



16
17
18
19
20
21
# File 'lib/zabbirc/setting.rb', line 16

def restore stored_options
  stored_options = ActiveSupport::HashWithIndifferentAccess.new stored_options
  unknown_keys = stored_options.keys - DEFAULTS.keys
  stored_options.delete_if{|k,_v| unknown_keys.include? k }
  @options = DEFAULTS.deep_dup.merge(stored_options)
end

#set(name, value, *options) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/zabbirc/setting.rb', line 23

def set name, value, *options
  host_group_id = parse_host_group_id(options)
  if host_group_id
    set_with_host_group name, value, host_group_id
  else
    @options[name] = value
  end
end

#to_hashObject



55
56
57
# File 'lib/zabbirc/setting.rb', line 55

def to_hash
  @options.to_hash.deep_dup
end