Class: Castled::Config

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

Defined Under Namespace

Classes: Error

Constant Summary collapse

CONFIG_FILENAME =
"config.yml"
DEFAULT_TEMPLATE =
<<~YAML
  backup_name: omarchy
  backup_paths:
    - ~/.bash_logout
    - ~/.bash_profile
    - ~/.bashrc
    - ~/.gemrc
    - ~/.XCompose
  destination: /run/media/leonid/250GB/backups
YAML

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Returns a new instance of Config.



40
41
42
43
44
45
46
47
# File 'lib/castled/config.rb', line 40

def initialize(path)
  @path = Pathname.new(path)
  data = YAML.safe_load(@path.read, permitted_classes: [Symbol], aliases: true) || {}
  @backup_name = data["backup_name"] || data[:backup_name]
  @backup_paths = Array(data["backup_paths"] || data[:backup_paths])
  @destination = data["destination"] || data[:destination]
  validate!
end

Instance Attribute Details

#backup_nameObject (readonly)

Returns the value of attribute backup_name.



23
24
25
# File 'lib/castled/config.rb', line 23

def backup_name
  @backup_name
end

#backup_pathsObject (readonly)

Returns the value of attribute backup_paths.



23
24
25
# File 'lib/castled/config.rb', line 23

def backup_paths
  @backup_paths
end

#destinationObject (readonly)

Returns the value of attribute destination.



23
24
25
# File 'lib/castled/config.rb', line 23

def destination
  @destination
end

Class Method Details

.init!(dir: Dir.pwd) ⇒ Object

Raises:



25
26
27
28
29
30
31
# File 'lib/castled/config.rb', line 25

def self.init!(dir: Dir.pwd)
  path = Pathname.new(dir).join(CONFIG_FILENAME)
  raise Error, "#{CONFIG_FILENAME} already exists" if path.exist?

  path.write(DEFAULT_TEMPLATE)
  path.to_s
end

.load!(dir: Dir.pwd) ⇒ Object

Raises:



33
34
35
36
37
38
# File 'lib/castled/config.rb', line 33

def self.load!(dir: Dir.pwd)
  path = Pathname.new(dir).join(CONFIG_FILENAME)
  raise Error, "#{CONFIG_FILENAME} not found. Run `simple-backup init` first." unless path.exist?

  new(path)
end

Instance Method Details

#destination_pathObject



49
50
51
# File 'lib/castled/config.rb', line 49

def destination_path
  Pathname.new(@destination).expand_path
end