Class: Braid::Config

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

Defined Under Namespace

Classes: MirrorDoesNotExist, PathAlreadyInUse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = CONFIG_FILE, old_config_files = [OLD_CONFIG_FILE]) ⇒ Config

Returns a new instance of Config.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/braid/config.rb', line 18

def initialize(config_file = CONFIG_FILE, old_config_files = [OLD_CONFIG_FILE])
  @config_file = config_file
  (old_config_files + [config_file]).each do |file|
    next unless File.exist?(file)
    begin
      store = YAML::Store.new(file)
      @db = {}
      store.transaction(true) do
        store.roots.each do |path|
          @db[path] = store[path]
        end
      end
      return
    rescue
      @db = JSON.parse(file)
      return if @db
    end
  end
  @db = {}
end

Instance Method Details

#add(mirror) ⇒ Object

Raises:



62
63
64
65
# File 'lib/braid/config.rb', line 62

def add(mirror)
  raise PathAlreadyInUse, mirror.path if get(mirror.path)
  write_mirror(mirror)
end

#add_from_options(url, options) ⇒ Object



39
40
41
42
43
44
# File 'lib/braid/config.rb', line 39

def add_from_options(url, options)
  mirror = Mirror.new_from_options(url, options)

  add(mirror)
  mirror
end

#get(path) ⇒ Object



50
51
52
53
54
# File 'lib/braid/config.rb', line 50

def get(path)
  key = path.to_s.sub(/\/$/, '')
  attributes = @db[key]
  attributes ? Mirror.new(path, attributes) : nil
end

#get!(path) ⇒ Object

Raises:



56
57
58
59
60
# File 'lib/braid/config.rb', line 56

def get!(path)
  mirror = get(path)
  raise MirrorDoesNotExist, path unless mirror
  mirror
end

#mirrorsObject



46
47
48
# File 'lib/braid/config.rb', line 46

def mirrors
  @db.keys
end

#remove(mirror) ⇒ Object



67
68
69
70
# File 'lib/braid/config.rb', line 67

def remove(mirror)
  @db.delete(mirror.path)
  write_db
end

#update(mirror) ⇒ Object

Raises:



72
73
74
75
76
# File 'lib/braid/config.rb', line 72

def update(mirror)
  raise MirrorDoesNotExist, mirror.path unless get(mirror.path)
  @db.delete(mirror.path)
  write_mirror(mirror)
end