Class: Snarl::SNP::Config::Normalize

Inherits:
Object
  • Object
show all
Defined in:
lib/snarl/snp/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k, v) ⇒ Normalize



33
34
35
# File 'lib/snarl/snp/config.rb', line 33

def initialize(k, v)
  @key, @value = k, v
end

Class Method Details

.store(k, v, hash) ⇒ Object



28
29
30
31
# File 'lib/snarl/snp/config.rb', line 28

def self.store(k, v, hash)
  nkey, nvalue = new(k, v).normalize
  hash.store(nkey, nvalue) if nkey && nvalue != nil
end

Instance Method Details

#normalizeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/snarl/snp/config.rb', line 37

def normalize
  # NOTE: [xxx, nil] is not set to @config
  # TODO : [k, v] should be equal to {k ,v}
  v = @value
  case @key.to_s
  when 'host' then
    ['host', if is_yaml_undef?(v) then nil else v end]
  when 'port' then
    # {:port => '9887'} is {'port' => 9887}
    ['port', if is_yaml_undef?(v) then nil else v.to_i end]
  when 'app', 'application', 'name', 'register' then
    ['app', if is_yaml_undef?(v) then nil else v end]
  when 'class', 'add_class', 'classes' then
    # {:class => 'cls'} and {'add_class' => {'cls' => nil}} are {'class' => ['cls', nil]}
    ['class', extract_classes(v)]
  when 'title' then
    ['title', if is_yaml_undef?(v) then nil else v end]
  when 'text', 'body', 'msg' then
    ['text', if is_yaml_undef?(v) then nil else v end]
  when 'timeout', 'duration', 'sec' then
    ['timeout', if is_yaml_undef?(v) then 0 else v.to_i end] # "timeout: nil" is sticky
  when 'sticky' then
    ['timeout', if is_yaml_false?(v) then nil else 0 end]
  when 'icon' then # NOTE: is "this notification icon", not "iconset setting"
    ['icon', if is_yaml_undef?(v) then nil else v end]
  when 'notification', 'notify', 'message' then
    ['notification', if is_yaml_undef?(v) then nil else v end]
  when 'unregister' then
    ['unregister', if is_yaml_false?(v) then false else true end]
  when 'iconset' then
    ['iconset', if is_yaml_undef?(v) then nil else v end]

  when 'log', 'logger' then # useless on yaml?
    ['logger', v]
  when 'logfile' then
    ['logfile', extract_logfile(v)]
  when 'loglevel', 'logger.level', 'log_level' then
    ['loglevel', extract_loglevel(v)]

  when 'config' then
    ['config', if is_yaml_undef?(v) then nil else v end]
  when 'yaml' then
    ['yaml', if is_yaml_undef?(v) then nil else v end]

  else [@key.to_s, v] # or raise?
  end
end