Class: Bup::Config

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

Overview

Configuration management.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bup/config.rb', line 13

def initialize
  @timeformat = "%Y-%m-%dT%H:%M:%S %z"
  @runtime = {
    "profile" => "default",
    "type" => "full"
  }

  @config = {
    "profiles" => {
      "default" => {
      }
    }
  }
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



11
12
13
# File 'lib/bup/config.rb', line 11

def config
  @config
end

#runtimeObject

Returns the value of attribute runtime.



11
12
13
# File 'lib/bup/config.rb', line 11

def runtime
  @runtime
end

Class Method Details

.merge(c1, c2) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bup/config.rb', line 63

def self.merge(c1, c2)
  (c1.keys + c2.keys)
    .each_with_object({}) do |r, l|
      l[r] = 1
    end.each_key do |key|
      v1 = c1[key]
      v2 = c2[key]
      if v1.nil?
        if v2.nil?
          # nop
        else
          c1[key] = v2
        end
      elsif v2.nil?
        # Nop - c1[key] already equals v1.
      elsif v1.instance_of?(Array) && v2.instance_of?(Array)
        c1[key] = v1 + v2
      elsif v1.instance_of?(Hash) && v2.instance_of?(Hash)
        c1[key] = merge(v1, v2)
      elsif !v2.nil?
        c1[key] = v2
      end
    end
  c1
end

Instance Method Details

#lastrun(name) ⇒ Object

Return the last run time of the backup or nil if there is none.



48
49
50
51
52
# File 'lib/bup/config.rb', line 48

def lastrun(name)
  DateTime.strptime(profile(name)["lastrun"] || "", @timeformat)
rescue Date::Error
  nil
end

#load(file) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/bup/config.rb', line 28

def load(file)
  c = File.open(file) do |io|
    YAML.safe_load(io)
  end

  Config.merge(@config, c)
end

#profile(name) ⇒ Object



59
60
61
# File 'lib/bup/config.rb', line 59

def profile(name)
  @config["profiles"][name] || (raise RuntimeError("No such profile #{name}."))
end

#save(file) ⇒ Object



36
37
38
39
40
# File 'lib/bup/config.rb', line 36

def save(file)
  File.open(file, "w") do |io|
    io.write(YAML.dump(@config))
  end
end

#set_lastrun(name, date) ⇒ Object

Return the last run time of the backup or nil if there is none.



55
56
57
# File 'lib/bup/config.rb', line 55

def set_lastrun(name, date)
  profile(name)["lastrun"] = date.strftime(@timeformat)
end

#update_lastrun(name) ⇒ Object



42
43
44
45
# File 'lib/bup/config.rb', line 42

def update_lastrun(name)
  date = DateTime.now.new_offset(0)
  set_lastrun(name, date)
end