Class: BConfig

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

Overview

high level configuration

Constant Summary collapse

YAMLCONF =
ENV['HOME'] + '/.booker.yml'

Instance Method Summary collapse

Constructor Details

#initializeBConfig

Returns a new instance of BConfig.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/config.rb', line 56

def initialize
  # config defaults (for osx, default chrome profile)
  @config = {
    :searcher  => "https://duckduckgo.com/?q=",
    :bookmarks => ENV['HOME'] +
    "/Library/Application Support/Google/Chrome/Profile 1/Bookmarks",
  }

  # configure through users yaml config file
  @config = read(YAMLCONF)

  valid = @config.keys
  @config.each do |k,v|
    @config[k.to_sym] = v if valid.include? k.to_sym
  end
end

Instance Method Details

#bookmarksObject



97
98
99
# File 'lib/config.rb', line 97

def bookmarks
  @config['bookmarks']
end

#read(file) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/config.rb', line 73

def read(file)
  begin
    @config = YAML::load(IO.read(file))
  rescue Errno::ENOENT
    puts "Warning: ".yel +
      "YAML configuration file couldn't be found. Using defaults."
    puts "Suggest: ".grn +
      "web --install config"
  rescue Psych::SyntaxError
    puts "Warning: ".red +
      "YAML configuration file contains invalid syntax. Using defaults."
  end
  @config
end

#searcherObject



101
102
103
# File 'lib/config.rb', line 101

def searcher
  @config['searcher']
end

#write(k = nil, v = nil) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/config.rb', line 88

def write(k=nil, v=nil)
  if k.nil? or v.nil?
    File.open(YAMLCONF, 'w') {|f| f.write(@config.to_yaml) }
  else
    @config[k] = v
    File.open(YAMLCONF, 'w+') {|f| f.write(@config.to_yaml) }
  end
end