Class: CWG::ConfigFile

Inherits:
Object
  • Object
show all
Defined in:
/Users/martyn/cw/cw_clone/lib/cw/config_file.rb

Constant Summary

CONFIG =
".cw_config"
HERE =
File.dirname(__FILE__) + '/'
CONFIGS =
['wpm', 'book_name', 'book_dir',
'play_command', 'success_colour', 'fail_colour',
'list_colour', 'ebook2cw_path', 'run_default']

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) config (readonly)

Returns the value of attribute config



11
12
13
# File '/Users/martyn/cw/cw_clone/lib/cw/config_file.rb', line 11

def config
  @config
end

Instance Method Details

- (Object) apply_config(sf)



59
60
61
62
63
64
65
66
67
68
69
70
# File '/Users/martyn/cw/cw_clone/lib/cw/config_file.rb', line 59

def apply_config(sf)
  cfg = read_config_maybe
  if cfg
    cfg.each_pair do |method, arg|
      begin
        sf.send("#{method}", arg)
      rescue
      end
      Params.send("#{method}=", arg)
    end
  end
end

- (Object) extract_config(line)



33
34
35
36
37
38
39
40
# File '/Users/martyn/cw/cw_clone/lib/cw/config_file.rb', line 33

def extract_config line
  CONFIGS.each do |cfg|
    if match_config?(line, cfg + ':')
      write_config(cfg, line)
      return
    end
  end
end

- (Boolean) match_config?(line, cfg)

Returns:

  • (Boolean)


25
26
27
28
29
30
31
# File '/Users/martyn/cw/cw_clone/lib/cw/config_file.rb', line 25

def match_config?(line, cfg)
  if line
    tmp = line.strip()[0, cfg.length]
    return true if(tmp == cfg)
  end
  false
end

- (Object) read_config



42
43
44
45
46
47
48
49
# File '/Users/martyn/cw/cw_clone/lib/cw/config_file.rb', line 42

def read_config
  File.open(CONFIG,'r') do |f|
    lines = readlines(f)
    lines.each do |line|
      extract_config line
    end
  end
end

- (Object) read_config_maybe



51
52
53
54
55
56
57
# File '/Users/martyn/cw/cw_clone/lib/cw/config_file.rb', line 51

def read_config_maybe
  if File.exist?(CONFIG)
    puts 'Loading config.'
    read_config
    config
  end
end

- (Object) readlines(f)



17
18
19
# File '/Users/martyn/cw/cw_clone/lib/cw/config_file.rb', line 17

def readlines f
  f.readlines
end

- (Object) write_config(cfg, line)



21
22
23
# File '/Users/martyn/cw/cw_clone/lib/cw/config_file.rb', line 21

def write_config(cfg, line)
  config[cfg.to_sym] = line.gsub(cfg + ':', '').strip
end