Class: Ppl::Adapter::Storage::Git

Inherits:
Ppl::Adapter::Storage show all
Defined in:
lib/ppl/adapter/storage/git.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ppl::Adapter::Storage

#require_contact

Constructor Details

#initialize(disk) ⇒ Git

Returns a new instance of Git.



11
12
13
14
# File 'lib/ppl/adapter/storage/git.rb', line 11

def initialize(disk)
  @disk = disk
  @repository = Rugged::Repository.new(@disk.directory.path)
end

Instance Attribute Details

#diskObject

Returns the value of attribute disk.



7
8
9
# File 'lib/ppl/adapter/storage/git.rb', line 7

def disk
  @disk
end

#repositoryObject

Returns the value of attribute repository.



8
9
10
# File 'lib/ppl/adapter/storage/git.rb', line 8

def repository
  @repository
end

#vcard_adapterObject

Returns the value of attribute vcard_adapter.



9
10
11
# File 'lib/ppl/adapter/storage/git.rb', line 9

def vcard_adapter
  @vcard_adapter
end

Class Method Details

.create_address_book(path) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/ppl/adapter/storage/git.rb', line 16

def self.create_address_book(path)
  repo = Rugged::Repository.init_at(path, false)
  disk = Ppl::Adapter::Storage::Disk.create_address_book(path)

  git = self.new(disk)
  git.add(".ppl/config")
  git.commit("first commit")
end

Instance Method Details

#add(file) ⇒ Object



71
72
73
74
# File 'lib/ppl/adapter/storage/git.rb', line 71

def add(file)
  @repository.index.add(file)
  @repository.index.write
end

#commit(message) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ppl/adapter/storage/git.rb', line 76

def commit(message)
  hash = @repository.index.write_tree
  tree = @repository.lookup hash
  head = nil

  begin
    head = @repository.head
  rescue
  end

  if !head.nil?
    parents    = [ @repository.lookup( @repository.head.target ).oid ]
    update_ref = "HEAD"
  else
    parents    = []
    update_ref = "HEAD"
  end

  name = ENV['USER']
  host = Socket.gethostname

  author = {
    :email => name + "@" + host,
    :time  => Time.now,
    :name  => name,
  }

  data = {
    :author     => author,
    :message    => message,
    :committer  => author,
    :tree       => tree,
    :parents    => parents,
    :update_ref => update_ref,
  }

  Rugged::Commit.create(@repository, data)
  true
end

#delete_contact(contact) ⇒ Object



63
64
65
66
67
68
# File 'lib/ppl/adapter/storage/git.rb', line 63

def delete_contact(contact)
  @repository.index.remove("#{contact.id}.vcf")
  @repository.index.write
  commit("remove_contact(#{contact.id})")
  @disk.delete_contact(contact)
end

#load_address_bookObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ppl/adapter/storage/git.rb', line 25

def load_address_book
  address_book = Ppl::Entity::AddressBook.new

  head = @repository.lookup(@repository.head.target)
  head.tree.each do |file|
    extension = file[:name].slice(-4..-1)
    if extension != ".vcf"
      next
    end
    contact_id = file[:name].slice(0..-5)
    contact    = load_contact(contact_id)
    address_book.add_contact(contact)
  end

  return address_book
end

#load_contact(id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ppl/adapter/storage/git.rb', line 42

def load_contact(id)
  filename = id + ".vcf"
  target   = @repository.head.target
  vcard    = @repository.file_at(target, filename)
  contact  = nil

  if !vcard.nil?
    contact    = @vcard_adapter.decode(vcard)
    contact.id = id
  end

  return contact
end

#save_contact(contact) ⇒ Object



56
57
58
59
60
61
# File 'lib/ppl/adapter/storage/git.rb', line 56

def save_contact(contact)
  @disk.save_contact(contact)

  add("#{contact.id}.vcf")
  commit("save_contact(#{contact.id})")
end