Class: Tonto::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/tonto/repo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repo

Returns a new instance of Repo.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tonto/repo.rb', line 7

def initialize(path)

  @path = path

  if File.exist?(@path) == false
    Grit::Repo.init(@path)
  end

  @db = Grit::Repo.new(path)
  @index = Grit::Index.new(@db)

  # Loads the list of known documents
  # !!! Find a better way to do this !!!
  @db.commits.any? ? ids = tri.contents.map {|c| c.name.to_i} : ids = []
  @ids = ids
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



5
6
7
# File 'lib/tonto/repo.rb', line 5

def db
  @db
end

#idsObject

Returns the value of attribute ids.



5
6
7
# File 'lib/tonto/repo.rb', line 5

def ids
  @ids
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/tonto/repo.rb', line 5

def path
  @path
end

Instance Method Details

#add(doc) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/tonto/repo.rb', line 43

def add(doc)
  @index.add(File.join(doc[:id].to_s, "attributes.json"), JSON.pretty_generate(doc))
  if doc.has_key?(:blobs) && doc[:blobs].is_a?(Hash)
    doc[:blobs].each do |k,v|
      @index.add(File.join(doc[:id].to_s, k), v)
    end
  end
  @index.commit("document #{doc[:id]}")
end

#exist?(id) ⇒ Boolean Also known as: exists?

Returns:

  • (Boolean)


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

def exist? id
  @ids.include?(id)
end

#get(id) ⇒ Object



28
29
30
31
# File 'lib/tonto/repo.rb', line 28

def get(id)
  object = tri / File.join(id.to_s, "attributes.json")
  return JSON.parse(object.data, :max_nesting => false)
end

#put(doc) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/tonto/repo.rb', line 33

def put(doc)
  if @ids.include?(doc[:id])
    old = get(doc[:id].to_s)
    doc.merge!(old)
    add(doc)
  else
    add(doc)
  end
end

#remove(id) ⇒ Object

HORRIBLE!!!11!1!



54
55
56
57
# File 'lib/tonto/repo.rb', line 54

def remove(id)
  #@index.delete(File.join(id.to_s, "attributes.json"))
  raise "NotImplemented"
end

#triObject



24
25
26
# File 'lib/tonto/repo.rb', line 24

def tri
  @db.commits.first.nil? ? [] : (@db.commits.first).tree
end