Class: Gaga

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

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Gaga

Returns a new instance of Gaga.



7
8
9
10
11
12
# File 'lib/gaga.rb', line 7

def initialize(options = {})
  @options = options
  unless ::File.exists?(File.join(path,'.git'))
    Grit::Repo.init(path)
  end
end

Instance Method Details

#[](key) ⇒ Object

Shortcut for #get

Example:

@store['key']  #=> value


52
53
54
# File 'lib/gaga.rb', line 52

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object

Shortcut for #set

Example:

@store[key] = 'value'


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

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

#clearObject

Deletes all contents of the store

Returns nothing



80
81
82
83
84
85
86
87
88
# File 'lib/gaga.rb', line 80

def clear
  save("all clear") do |index|
    if tree = index.current_tree
      tree.contents.each do |entry|
        index.delete(key_for(entry.name))
      end
    end
  end
end

#delete(key) ⇒ Object

Deletes commits matching the given key

Example:

@store.delete('key')

Returns nothing



71
72
73
74
75
# File 'lib/gaga.rb', line 71

def delete(key, *)
  self[key].tap do
    save("deleted #{key}") {|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



41
42
43
44
45
# File 'lib/gaga.rb', line 41

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)


106
107
108
# File 'lib/gaga.rb', line 106

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']


61
62
63
# File 'lib/gaga.rb', line 61

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



96
97
98
# File 'lib/gaga.rb', line 96

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

#set(key, value) ⇒ Object

Add the value to the to the store

Example

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

Returns nothing



20
21
22
23
24
# File 'lib/gaga.rb', line 20

def set(key, value)
  save("set '#{key}'") do |index|
    index.add(key_for(key), encode(value))
  end
end