Class: BConfig

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

Overview

configuration

Constant Summary collapse

VALID =
[:searcher, :bookmarks, :browser]
HOME =
ENV['HOME']
YAMLCONF =
HOME + '/.booker.yml'

Instance Method Summary collapse

Constructor Details

#initializeBConfig

Returns a new instance of BConfig.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/config.rb', line 59

def initialize
  # config defaults (for osx, default chrome profile)
  readyaml = read(YAMLCONF)
  default_config = {
    :browser  => 'open ',
    :searcher  => "https://google.com/?q=",
    :bookmarks => HOME +
    "/Library/Application Support/Google/Chrome/Profile 1/Bookmarks",
  }

  # configure w/ yaml config file, if it exists
  @config = readyaml ? readyaml : default_config

  # prune bad config keys
  @config.each do |k, v|
    if !VALID.include? k.to_sym
      puts "Failure:".red + " Bad key found in config file: #{k}"
      exit 1
    end
  end
end

Instance Method Details

#bookmarksObject



107
108
109
# File 'lib/config.rb', line 107

def bookmarks
  @config[:bookmarks]
end

#read(file) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/config.rb', line 81

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 + "booker --install config"
    return false
  rescue Psych::SyntaxError
    puts "Warning: ".red +
      "YAML configuration file contains invalid syntax. Using defaults."
    return false
  end
  config
end

#searcherObject



111
112
113
# File 'lib/config.rb', line 111

def searcher
  @config[:searcher]
end

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

used for creating and updating the default configuration



98
99
100
101
102
103
104
105
# File 'lib/config.rb', line 98

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