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



92
93
94
95
96
97
98
99
# File 'lib/gl_tail/config/yaml_parser.rb', line 92

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



61
62
63
64
65
66
67
68
# File 'lib/gl_tail/config/yaml_parser.rb', line 61

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



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

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

#parse_configObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gl_tail/config/yaml_parser.rb', line 70

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
47
48
49
50
51
52
53
# File 'lib/gl_tail/config/yaml_parser.rb', line 34

def parse_servers

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

    name = server.shift
    data = server.shift

    if data['source'] && data['source'].downcase == 'local'
      src = GlTail::Source::Local.new(@config)
    else 
      src = GlTail::Source::SSH.new(@config)
    end
    
    src.name = name

    apply_values(src, data)

    @config.sources << src
  end
end