Class: SnapshotArchive::Cfg

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCfg

Returns a new instance of Cfg.



67
68
69
# File 'lib/snapshot_archive/cfg.rb', line 67

def initialize
  self.active_stores = []
end

Instance Attribute Details

#active_storesObject

Returns the value of attribute active_stores.



66
67
68
# File 'lib/snapshot_archive/cfg.rb', line 66

def active_stores
  @active_stores
end

#pwdObject

Returns the value of attribute pwd.



66
67
68
# File 'lib/snapshot_archive/cfg.rb', line 66

def pwd
  @pwd
end

#shellObject

Returns the value of attribute shell.



66
67
68
# File 'lib/snapshot_archive/cfg.rb', line 66

def shell
  @shell
end

#storage_pathObject

Returns the value of attribute storage_path.



66
67
68
# File 'lib/snapshot_archive/cfg.rb', line 66

def storage_path
  @storage_path
end

Class Method Details

.instanceObject



45
46
47
# File 'lib/snapshot_archive/cfg.rb', line 45

def instance
  @instance ||= Cfg.new
end

Instance Method Details

#bind_backup(name, args) ⇒ Object



125
126
127
# File 'lib/snapshot_archive/cfg.rb', line 125

def bind_backup(name, args)
  SnapshotArchive::Stores::BoundBackup.new(store_registry.fetch(name), args)
end

#load(path = ".config/snapshot_archive.rb") ⇒ Object



71
72
73
74
75
76
# File 'lib/snapshot_archive/cfg.rb', line 71

def load(path = ".config/snapshot_archive.rb")
  config_path = File.join(ENV["HOME"], path)

  require("snapshot_archive/default_configuration")
  Kernel.load(config_path) if File.exist?(config_path)
end

#parse_store(str) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/snapshot_archive/cfg.rb', line 109

def parse_store(str)
  name, *store_args = str.split(/[:,]/)

  store = (
    if store_args.count > 0
      bind_backup(name, store_args)
    else
      store_registry.fetch(name)
    end
  )

  [name, store]
rescue KeyError
  raise "Store not found: '#{str}'"
end

#register_store(name, klass_or_alias = nil, active_by_default: true, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/snapshot_archive/cfg.rb', line 86

def register_store(name, klass_or_alias=nil, active_by_default: true, &block)
  store = (
    if klass_or_alias.is_a?(String)
      parse_store(klass_or_alias)[1]
    elsif klass_or_alias
      klass_or_alias
    else
      builder = StoreBuilder.new
      yield(builder)
      builder.to_store
    end
  )

  store_registry[name] = store
  if active_by_default
    active_stores << name
  end
end

#repositoryObject



105
106
107
# File 'lib/snapshot_archive/cfg.rb', line 105

def repository
  Repositories::FileSystem.new(path: storage_path)
end

#resolve_alias(name) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/snapshot_archive/cfg.rb', line 78

def resolve_alias(name)
  if store_registry[name].is_a?(Array)
    store_registry[name]
  else
    name
  end
end

#store(name) ⇒ Object



129
130
131
# File 'lib/snapshot_archive/cfg.rb', line 129

def store(name)
  store_registry.fetch(name)
end

#storesObject



133
134
135
136
137
138
139
140
141
# File 'lib/snapshot_archive/cfg.rb', line 133

def stores
  unknown_keys = active_stores - store_registry.keys

  if !unknown_keys.empty?
    raise ArgumentError.new("invalid store(s): #{unknown_keys.join(",")}")
  end

  store_registry.slice(*active_stores)
end