Class: Gaga

Inherits:
Object
  • Object
show all
Defined in:
lib/gaga.rb,
lib/gaga/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Gaga

Returns a new instance of Gaga.


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/gaga.rb', line 7

def initialize(options = {})
  @author = options.delete(:author)
  @committer = options.delete(:committer)
  @options = options
  
  if path.end_with?('.git/')
    Grit::Repo.init_bare(path) unless File.exists?(File.join(path,'refs'))
  else
    Grit::Repo.init(path) unless File.exists?(File.join(path,'.git'))
  end
end

Instance Method Details

#[](key) ⇒ Object

Shortcut for #get

Example:

@store['key']  #=> value

59
60
61
# File 'lib/gaga.rb', line 59

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object

Shortcut for #set

Example:

@store[key] = 'value'

38
39
40
# File 'lib/gaga.rb', line 38

def []=(key, value)
  set(key, value)
end

#clear(opts = {}) ⇒ Object

Deletes all contents of the store

Returns nothing


88
89
90
91
92
93
94
95
96
# File 'lib/gaga.rb', line 88

def clear(opts = {})    
  save(setup_commit_options({:message => "all clear"}.merge(opts))) do |index|
    if tree = index.current_tree
      tree.contents.each do |entry|
        index.delete(key_for(entry.name))
      end
    end
  end
end

#delete(key, opts = {}) ⇒ Object

Deletes commits matching the given key

Example:

@store.delete('key')

Returns nothing


78
79
80
81
82
83
# File 'lib/gaga.rb', line 78

def delete(key, opts = {})
  options = setup_commit_options({:message => "deleted #{key}"}.merge(opts))
  self[key].tap do
    save(options) {|index| index.delete(key_for(key)) }
  end
end

#get(key, value = nil) ⇒ Object

Retrieve the value for the given key with a default value

Example:

@store.get(key)  #=> value

Returns the object found in the repo matching the key


48
49
50
51
52
# File 'lib/gaga.rb', line 48

def get(key, value = nil, *)
  if head && blob = head.commit.tree / key_for(key)
    decode(blob.data)
  end
end

#key?(key) ⇒ Boolean

Find the key if exists in the git repo

Example:

@store.key? 'key'  #=> true

Returns true if found; false if not found

Returns:

  • (Boolean)

114
115
116
# File 'lib/gaga.rb', line 114

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

#keysObject

Returns an array of key names contained in store

Example:

@store.keys  #=> ['key1', 'key2']

68
69
70
# File 'lib/gaga.rb', line 68

def keys
  head.commit.tree.contents.map{|blob| deserialize(blob.name) }
end

#log(key) ⇒ Object

The commit log for the given key

Example:

@store.log('key') #=> [{"message"=>"Updated key"...}]

Returns Array of commit data


104
105
106
# File 'lib/gaga.rb', line 104

def log(key)
  git.log(branch, key_for(key)).map{ |commit| commit.to_hash }
end

#set(key, value, opts = {}) ⇒ Object

Add the value to the to the store

Example

@store.set('key', 'value')

Returns nothing


25
26
27
28
29
30
31
# File 'lib/gaga.rb', line 25

def set(key, value, opts = {})
  unless value == get(key)
    save(setup_commit_options({:message => "set '#{key}'"}.merge(opts))) do |index|
      index.add(key_for(key), encode(value))
    end
  end
end