Class: GlTail::YamlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gl_tail/config/yaml_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ YamlParser

Returns a new instance of YamlParser.



7
8
9
10
# File 'lib/gl_tail/config/yaml_parser.rb', line 7

def initialize file
  file  ||= "config.yaml"
  @yaml   = YAML.load_file(file)
end

Instance Attribute Details

#yamlObject (readonly)

Returns the value of attribute yaml.



5
6
7
# File 'lib/gl_tail/config/yaml_parser.rb', line 5

def yaml
  @yaml
end

Instance Method Details

#add_blocks(column, hash) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/gl_tail/config/yaml_parser.rb', line 85

def add_blocks(column, hash)
  hash.each do |key, config|
    block = @config.add_block(key)
    block.column = column
    
    apply_values(block, config)
  end
end

#apply(config) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gl_tail/config/yaml_parser.rb', line 12

def apply(config)
  @config = config

  @left   = Hash.new
  @right  = Hash.new
  @blocks = Array.new

  parse_servers
  parse_config
  parse_resolver

  @config
end

#apply_value(target, key, value) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/gl_tail/config/yaml_parser.rb', line 54

def apply_value(target, key, value)
  begin
    target.send("#{key}=", value)
  rescue
    puts "FAILED TO APPLY #{key}=#{value} TO #{target}"
    raise
  end
end

#apply_values(target, hash) ⇒ Object



48
49
50
51
52
# File 'lib/gl_tail/config/yaml_parser.rb', line 48

def apply_values(target, hash)
  hash.each do |key, value|
    apply_value(target, key, value)
  end
end

#parse_configObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gl_tail/config/yaml_parser.rb', line 63

def parse_config
  self.yaml['config'].each do |key, config|
    screen = @config.screen

    case config
    when Hash
      if key =~ /(left|right)_column/
        column = screen.send($1)
        blocks = config.delete('blocks')

        apply_values(column, config)
        add_blocks(column, blocks)
      else
        target = screen.send(key)          
        apply_values(target, config)
      end
    else
      apply_value(screen, key, config)
    end
  end
end

#parse_resolverObject



26
27
28
29
30
31
32
# File 'lib/gl_tail/config/yaml_parser.rb', line 26

def parse_resolver
  if x = self.yaml['resolver']
    x.each do |k, v|
      apply_value(Resolver.instance, k, v )
    end
  end
end

#parse_serversObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gl_tail/config/yaml_parser.rb', line 34

def parse_servers

  self.yaml['servers'].each do |server|

    src = GlTail::Source::SSH.new(@config)

    src.name = server.shift

    apply_values(src, server.shift)

    @config.sources << src
  end
end