Class: Knowledge::Backupper

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

Overview

Description

Object used to backup configuration variables.

Usage

@example:

backupper = Knowledge::Backupper.new

backupper.register(name: :foo, value: 'bar')
backupper.register(name: :bar, value: 'baz')

backupper.backup!

Attributes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ Backupper

Just sets the basic configuration object.

Parameters

Parameters:

  • :path (String)

    Path to the YAML file where to backup the config



47
48
49
50
# File 'lib/knowledge/backupper.rb', line 47

def initialize(path:)
  @configuration = {}
  @path = path
end

Instance Attribute Details

#configurationObject (readonly)

Configuration hash



29
30
31
# File 'lib/knowledge/backupper.rb', line 29

def configuration
  @configuration
end

#pathObject (readonly)

Backup file path



29
30
31
# File 'lib/knowledge/backupper.rb', line 29

def path
  @path
end

Instance Method Details

#backup!Object

Backups the configuration.



57
58
59
60
61
62
63
# File 'lib/knowledge/backupper.rb', line 57

def backup!
  f = File.new(path, 'w')

  f.write(configuration.to_yaml)

  f.close
end

#set(name:, value:) ⇒ Object Also known as: register

Sets the variable before backuping everything.

Parameters

Parameters:

  • :name (String | Symbol)
  • :value (Any)


73
74
75
# File 'lib/knowledge/backupper.rb', line 73

def set(name:, value:)
  configuration[name.to_sym] = value
end