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.



17
18
19
# File 'lib/braid/config.rb', line 17

def initialize(config_file = CONFIG_FILE)
  @db = YAML::Store.new(config_file)
end

Instance Method Details

#add(mirror) ⇒ Object



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

def add(mirror)
  @db.transaction do
    raise PathAlreadyInUse, mirror.path if @db[mirror.path]
    write_mirror(mirror)
  end
end

#add_from_options(url, options) ⇒ Object



21
22
23
24
25
26
# File 'lib/braid/config.rb', line 21

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

  add(mirror)
  mirror
end

#get(path) ⇒ Object



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

def get(path)
  @db.transaction(true) do
    if attributes = @db[path.to_s.sub(/\/$/, '')]
      Mirror.new(path, attributes)
    end
  end
end

#get!(path) ⇒ Object

Raises:



42
43
44
45
46
# File 'lib/braid/config.rb', line 42

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

#migrate!Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/braid/config.rb', line 77

def migrate!
  @db.transaction do
    @db.roots.each do |path|
      attributes = @db[path]
      if attributes["local_branch"]
        attributes["url"]      = attributes.delete("remote")
        attributes["remote"]   = attributes.delete("local_branch")
        attributes["squashed"] = attributes.delete("squash")
        attributes["lock"]     = attributes["revision"] # so far this has always been true
      end
      @db[path] = clean_attributes(attributes)
    end
  end
end

#mirrorsObject



28
29
30
31
32
# File 'lib/braid/config.rb', line 28

def mirrors
  @db.transaction(true) do
    @db.roots
  end
end

#remove(mirror) ⇒ Object



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

def remove(mirror)
  @db.transaction do
    @db.delete(mirror.path)
  end
end

#update(mirror) ⇒ Object



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

def update(mirror)
  @db.transaction do
    raise MirrorDoesNotExist, mirror.path unless @db[mirror.path]
    @db.delete(mirror.path)
    write_mirror(mirror)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
# File 'lib/braid/config.rb', line 69

def valid?
  @db.transaction(true) do
    !@db.roots.any? do |path|
      @db[path]["url"].nil?
    end
  end
end