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.



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

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

Instance Attribute Details

#diskObject

Returns the value of attribute disk.



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

def disk
  @disk
end

#repositoryObject

Returns the value of attribute repository.



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

def repository
  @repository
end

#vcard_adapterObject

Returns the value of attribute vcard_adapter.



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

def vcard_adapter
  @vcard_adapter
end

Class Method Details

.create_address_book(path) ⇒ Object



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

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



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

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

#commit(message) ⇒ Object



69
70
71
72
73
74
75
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
# File 'lib/ppl/adapter/storage/git.rb', line 69

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 ).oid ]
    update_ref = "HEAD"
  else
    parents    = []
    update_ref = "HEAD"
  end

  begin
    git_config = Rugged::Config.open_global.to_hash
  rescue Rugged::OSError
    git_config = {}
  end

  author = {
    :email => git_config.fetch('user.email', "#{ENV['USER']}@#{Socket.gethostname}"),
    :time  => Time.now,
    :name  => git_config.fetch('user.name', ENV['USER']),
  }

  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



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

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



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

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

  head = @repository.lookup(@repository.head.target.oid)
  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.contacts.push(contact)
  end

  return address_book
end

#load_contact(id) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/ppl/adapter/storage/git.rb', line 41

def load_contact(id)
  begin
    read_contact_from_disk id
  rescue Vpim::InvalidEncodingError
    raise Ppl::Error::InvalidVcard, "#{id}.vcf contains invalid data"
  end
end

#pathObject



112
113
114
# File 'lib/ppl/adapter/storage/git.rb', line 112

def path
  @disk.path
end

#save_contact(contact) ⇒ Object



49
50
51
52
53
54
# File 'lib/ppl/adapter/storage/git.rb', line 49

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

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