Module: Snarl::SNP::SNPProcedure

Included in:
Snarl::SNP
Defined in:
lib/snarl/snp/snp_procedure.rb

Instance Method Summary collapse

Instance Method Details

#apply_yaml(yamldata) ⇒ Object



64
65
66
67
68
# File 'lib/snarl/snp/snp_procedure.rb', line 64

def apply_yaml(yamldata)
  YAML.load(yamldata).each do |k ,v|
    self[k] = v
  end
end

#auto_add_classObject



13
14
15
16
17
# File 'lib/snarl/snp/snp_procedure.rb', line 13

def auto_add_class
  if is_app_available? && is_class_available? then
    add_classes(*self['class'])
  end
end

#auto_notificationObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/snarl/snp/snp_procedure.rb', line 20

def auto_notification
  _notify_arg_is_array = is_notification_available? && self['notification'].kind_of?(Array)
  _notify_arg_is_arraied_notify_hashes = is_notification_available? && self['notification'].first.kind_of?(Hash)

  if is_notification_available? then
    if _notify_arg_is_array then
      if _notify_arg_is_arraied_notify_hashes then
        # [{notify_hash1}, {notify_hash2}, ...]
        self['notification'].map{|h| notification(h)}
      else
        # [title, text, timeout, icon, class]
        notification(*self['notification'])
      end
    else
      # {notify_hash}
      notification(self['notification'])
    end
  else # config has no 'notification' key
    # build notify arg from config
    h = {}
    h[:app] = self['app'] if self['app']
    h[:title] = self['title'] if self['title']
    h[:text] = self['text'] if self['text']
    h[:timeout] = self['timeout'] if self['timeout']
    h[:icon] = self['icon'] if self['icon']
    h[:class] = self['class'][0][0] if is_class_available? # snp['class'] == [['clsnam', 'clstit']]
    notification(h) if (h.has_key?(:text)||h.has_key?(:title))
  end
end

#auto_registerObject



6
7
8
9
10
# File 'lib/snarl/snp/snp_procedure.rb', line 6

def auto_register
  if is_app_available? then
    register(self['app'])
  end
end

#auto_unregisterObject



52
53
54
55
56
# File 'lib/snarl/snp/snp_procedure.rb', line 52

def auto_unregister
  if self['unregister'] && is_app_available? then
    unregister(self['app'])
  end
end

#autoexecute_with_config!Object



58
59
60
61
62
# File 'lib/snarl/snp/snp_procedure.rb', line 58

def autoexecute_with_config!
  [auto_register,
   auto_add_class,
   auto_notification]
end

#load(yamldata, logger = nil, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/snarl/snp/snp_procedure.rb', line 86

def load(yamldata, logger = nil, &block)
  apply_yaml(yamldata)
  # apply/overwrite user Logger object
  # yaml cannot make 'raw' Logger object
  set_logger(logger)
  begin
    autoexecute_with_config!
    block.call(self) if block_given?
  ensure
    auto_unregister
  end
  self
end

#set_logger(arg_logger) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/snarl/snp/snp_procedure.rb', line 70

def set_logger(arg_logger)
  # TODO: support for another Logger
  if arg_logger then
    require 'logger' # FIXME: oh twice?
    arg_logger = Logger.new(arg_logger) unless arg_logger.respond_to?(:warn)
    self.logger = arg_logger
  elsif self['logfile'] then
    require 'logger' # FIXME: oh ...
    logger = Logger.new(self['logfile'])
    logger.level = self['loglevel'] if self['loglevel']
    self.logger = logger
  else
    # do nothing
  end
end