Class: Dakwak::Configurator

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/dakwak/configurator.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

included, #log, #log_indent, #log_indent_dec, #log_indent_inc, #logging_context

Methods included from Silencable

#silence, #silent?

Constructor Details

#initializeConfigurator

include Silencable



23
24
25
26
27
28
29
# File 'lib/dakwak/configurator.rb', line 23

def initialize
  logging_context("Configurator")
  @@subs ||= {}
  @@opts ||= {}

  super()
end

Class Attribute Details

.subsObject (readonly)

Returns the value of attribute subs.



8
9
10
# File 'lib/dakwak/configurator.rb', line 8

def subs
  @subs
end

Class Method Details

.subscribe(context, configurable) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/dakwak/configurator.rb', line 10

def subscribe(context, configurable)
  if not configurable.respond_to?("set_option")
    raise "Subscribing as a configurable must respond_to method 'set_option'"
  end

  @@subs          ||= {}
  @@subs[context] ||= []; @@subs[context] << configurable
end

Instance Method Details

#consume(json_feed) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dakwak/configurator.rb', line 31

def consume(json_feed)
  begin
    cfg = JSON.parse(json_feed)
  rescue JSON::ParserError => e
    log_error "Unable to parse JSON configuration feed; #{e.message}"
    return nil
  end


  cfg.each_pair { |ctx, options|

    if not @@subs[ctx] or @@subs[ctx].empty? then
      log_warn "Context #{ctx} has no subscribers, ignoring." unless silent?
      next
    end

    log_debug "Configuring context #{ctx} (#{@@subs[ctx].size} subscribers)" unless silent?

    options.each_pair { |name, val|
      @@subs[ctx] ||= []
      @@subs[ctx].each { |configurable|
        configurable.set_option(name, val)
      }
    }
  }

  true
end