Class: Tonto::Repo
- Inherits:
-
Object
- Object
- Tonto::Repo
- Defined in:
- lib/tonto/repo.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
-
#ids ⇒ Object
Returns the value of attribute ids.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #add(doc) ⇒ Object
- #exist?(id) ⇒ Boolean (also: #exists?)
- #get(id) ⇒ Object
-
#initialize(path) ⇒ Repo
constructor
A new instance of Repo.
- #put(doc) ⇒ Object
-
#remove(id) ⇒ Object
HORRIBLE!!!11!1!.
- #tri ⇒ Object
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
#db ⇒ Object
Returns the value of attribute db.
5 6 7 |
# File 'lib/tonto/repo.rb', line 5 def db @db end |
#ids ⇒ Object
Returns the value of attribute ids.
5 6 7 |
# File 'lib/tonto/repo.rb', line 5 def ids @ids end |
#path ⇒ Object
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?
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 |
#tri ⇒ Object
24 25 26 |
# File 'lib/tonto/repo.rb', line 24 def tri @db.commits.first.nil? ? [] : (@db.commits.first).tree end |