Class: Configura

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

Defined Under Namespace

Classes: AskForMissingValue, FailWhenMissingValue

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigura

Returns a new instance of Configura.



22
23
24
25
26
27
# File 'lib/configura.rb', line 22

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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

Instance Method Details

#[](key) ⇒ Object



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

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

#[]=(key, value) ⇒ Object



50
51
52
# File 'lib/configura.rb', line 50

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

#delete(*keys) ⇒ Object



54
55
56
57
58
# File 'lib/configura.rb', line 54

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

#ensure_file_existsObject



29
30
31
32
33
34
# File 'lib/configura.rb', line 29

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

#loadObject



36
37
38
39
# File 'lib/configura.rb', line 36

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

#saveObject



66
67
68
69
# File 'lib/configura.rb', line 66

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

#update(c = {}) ⇒ Object



60
61
62
63
64
# File 'lib/configura.rb', line 60

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