Class: Configura

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

Defined Under Namespace

Classes: AskForMissingValue, FailWhenMissingValue

Instance Method Summary collapse

Constructor Details

#initializeConfigura

Returns a new instance of Configura.



20
21
22
23
24
25
# File 'lib/configura.rb', line 20

def initialize()
  @file = "#{ENV['HOME']}/.configura"
  ensure_file_exists
  @missing_value_strategy = $stdout.tty? ? AskForMissingValue.new : FailWhenMissingValue.new
  load
end

Instance Method Details

#[](key) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/configura.rb', line 39

def [](key)
  load
  if @config[key].nil?
    @config[key] = @missing_value_strategy.seek(key)
    save
  end
  @config[key]
end

#[]=(key, value) ⇒ Object



48
49
50
# File 'lib/configura.rb', line 48

def []=(key, value)
  @config[key] = value
end

#delete(*keys) ⇒ Object



52
53
54
55
56
# File 'lib/configura.rb', line 52

def delete(*keys)
  keys.each { |key| @config.delete(key) }
  save
  self
end

#ensure_file_exists(file) ⇒ Object



27
28
29
30
31
32
# File 'lib/configura.rb', line 27

def ensure_file_exists(file)
  unless File.exists? file
    @config = {}
    save
  end
end

#loadObject



34
35
36
37
# File 'lib/configura.rb', line 34

def load
  @config ||= YAML::load(open(file))
  self
end

#saveObject



64
65
66
67
# File 'lib/configura.rb', line 64

def save
  File.open(file, 'w') { |f| f.write(YAML.dump(@config)) }
  self
end

#update(c = {}) ⇒ Object



58
59
60
61
62
# File 'lib/configura.rb', line 58

def update(c={})
  @config.merge!(c)
  save
  self
end