Module: Adapter::Git

Defined in:
lib/adapter/git.rb,
lib/adapter/git/version.rb

Constant Summary collapse

VERSION =
"0.5.0"

Instance Method Summary collapse

Instance Method Details

#branchObject



7
8
9
# File 'lib/adapter/git.rb', line 7

def branch
  options[:branch] || 'master'
end

#clearObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/adapter/git.rb', line 37

def clear
  commit("Cleared") do |index|
    tree = index.current_tree
    tree = tree / options[:path] if options[:path] && tree
    if tree
      tree.contents.each do |entry|
        index.delete(key_for(entry.name))
      end
    end
  end
end

#decode(value) ⇒ Object



53
54
55
# File 'lib/adapter/git.rb', line 53

def decode(value)
  YAML.load(value)
end

#delete(key) ⇒ Object



31
32
33
34
35
# File 'lib/adapter/git.rb', line 31

def delete(key)
  read(key).tap do
    commit("Delete #{key}") {|index| index.delete(key_for(key)) }
  end
end

#encode(value) ⇒ Object



49
50
51
# File 'lib/adapter/git.rb', line 49

def encode(value)
  value.to_yaml
end

#headObject



11
12
13
# File 'lib/adapter/git.rb', line 11

def head
  client.get_head(branch)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/adapter/git.rb', line 15

def key?(key)
  !(head && head.commit.tree / key_for(key)).nil?
end

#key_for(key) ⇒ Object



57
58
59
# File 'lib/adapter/git.rb', line 57

def key_for(key)
  File.join(*[options[:path], super].compact)
end

#read(key) ⇒ Object



19
20
21
22
23
# File 'lib/adapter/git.rb', line 19

def read(key)
  if head && blob = head.commit.tree / key_for(key)
    decode(blob.data)
  end
end

#write(key, value) ⇒ Object



25
26
27
28
29
# File 'lib/adapter/git.rb', line 25

def write(key, value)
  commit("Updated #{key}") do |index|
    index.add(key_for(key), encode(value))
  end
end