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.



31
32
33
# File 'lib/braid/config.rb', line 31

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

Instance Method Details

#add(mirror) ⇒ Object



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

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



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

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

  add(mirror)
  mirror
end

#get(path) ⇒ Object



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

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:



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

#migrate!Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/braid/config.rb', line 91

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



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

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

#remove(mirror) ⇒ Object



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

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

#update(mirror) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/braid/config.rb', line 75

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)


83
84
85
86
87
88
89
# File 'lib/braid/config.rb', line 83

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