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) ⇒ Config

Returns a new instance of Config.



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

def initialize(config_file = CONFIG_FILE)
  @config_file = config_file
  begin
    store = YAML::Store.new(@config_file)
    @db = {}
    store.transaction(true) do
      store.roots.each do |path|
        @db[path] = store[path]
      end
    end
  rescue
    @db = JSON.parse(@config_file)
  end
end

Instance Method Details

#add(mirror) ⇒ Object

Raises:



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

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

#add_from_options(url, options) ⇒ Object



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

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

  add(mirror)
  mirror
end

#get(path) ⇒ Object



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

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

#get!(path) ⇒ Object

Raises:



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

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

#mirrorsObject



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

def mirrors
  @db.keys
end

#remove(mirror) ⇒ Object



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

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

#update(mirror) ⇒ Object

Raises:



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

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