Module: Helpers

Included in:
DataMapper::Adapters::GitAdapter
Defined in:
lib/dm-git-adapter/helpers.rb

Instance Method Summary collapse

Instance Method Details

#create_db!Object


3
4
5
6
# File 'lib/dm-git-adapter/helpers.rb', line 3

def create_db!
  raise "Database #{@path} already exists!" if File.exist? @path
  Grit::Repo.init @path
end

#current_treeObject


12
13
14
15
# File 'lib/dm-git-adapter/helpers.rb', line 12

def current_tree
  c = last_c
  c ? c.tree : nil
end

#json_file(model) ⇒ Object


17
18
19
# File 'lib/dm-git-adapter/helpers.rb', line 17

def json_file(model)
  current_tree / File.join("#{model}","attributes.json")
end

#last_cObject


8
9
10
# File 'lib/dm-git-adapter/helpers.rb', line 8

def last_c
  @repo.commits("master^..master").first || @repo.commits("master").first
end

#records_for(model) ⇒ Object


21
22
23
24
# File 'lib/dm-git-adapter/helpers.rb', line 21

def records_for(model)
  file = json_file(model).data
  JSON.parse(file).dup || []
end

#write_records(model, attrs) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Helpers)

    the object that the method was called on


26
27
28
29
30
31
32
33
# File 'lib/dm-git-adapter/helpers.rb', line 26

def write_records(model, attrs, &block)
  parent = last_c
  self.index = Grit::Index.new(@repo)
  index.read_tree(parent.to_s)
  yield self
  committer = Grit::Actor.new("fyskij", "[email protected]")
  index.commit("lol", nil, committer, nil, "master")
end