Class: Ppl::Adapter::Storage::Disk
- Inherits:
-
Ppl::Adapter::Storage
- Object
- Ppl::Adapter::Storage
- Ppl::Adapter::Storage::Disk
- Defined in:
- lib/ppl/adapter/storage/disk.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#vcard_adapter ⇒ Object
Returns the value of attribute vcard_adapter.
Class Method Summary collapse
Instance Method Summary collapse
- #delete_contact(contact) ⇒ Object
- #filename_for_contact(contact) ⇒ Object
- #filename_for_contact_id(id) ⇒ Object
-
#initialize(directory) ⇒ Disk
constructor
A new instance of Disk.
- #load_address_book ⇒ Object
- #load_contact(id) ⇒ Object
- #path ⇒ Object
- #save_contact(contact) ⇒ Object
Methods inherited from Ppl::Adapter::Storage
Constructor Details
#initialize(directory) ⇒ Disk
Returns a new instance of Disk.
22 23 24 |
# File 'lib/ppl/adapter/storage/disk.rb', line 22 def initialize(directory) @directory = directory end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
5 6 7 |
# File 'lib/ppl/adapter/storage/disk.rb', line 5 def directory @directory end |
#vcard_adapter ⇒ Object
Returns the value of attribute vcard_adapter.
6 7 8 |
# File 'lib/ppl/adapter/storage/disk.rb', line 6 def vcard_adapter @vcard_adapter end |
Class Method Details
.create_address_book(path) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ppl/adapter/storage/disk.rb', line 8 def self.create_address_book(path) if !Dir.exists? path FileUtils.mkdir_p(path) end storage = self.new(Dir.new(path)) dot_ppl = File.join(path, ".ppl") config = File.join(dot_ppl, "config") FileUtils.mkdir_p(dot_ppl) FileUtils.touch(config) storage end |
Instance Method Details
#delete_contact(contact) ⇒ Object
63 64 65 |
# File 'lib/ppl/adapter/storage/disk.rb', line 63 def delete_contact(contact) File.unlink filename_for_contact(contact) end |
#filename_for_contact(contact) ⇒ Object
67 68 69 |
# File 'lib/ppl/adapter/storage/disk.rb', line 67 def filename_for_contact(contact) filename_for_contact_id(contact.id) end |
#filename_for_contact_id(id) ⇒ Object
71 72 73 |
# File 'lib/ppl/adapter/storage/disk.rb', line 71 def filename_for_contact_id(id) File.join(@directory.path, id + ".vcf") end |
#load_address_book ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ppl/adapter/storage/disk.rb', line 26 def load_address_book address_book = Ppl::Entity::AddressBook.new pattern = File.join @directory.path, "*.vcf" filenames = Dir.glob pattern filenames.each do |filename| contact_id = File.basename(filename).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 48 49 50 51 52 |
# File 'lib/ppl/adapter/storage/disk.rb', line 41 def load_contact(id) filename = filename_for_contact_id(id) contact = nil if File.exists?(filename) vcard = File.read filename contact = @vcard_adapter.decode(vcard) if !contact.nil? && contact.is_a?(Ppl::Entity::Contact) contact.id = id end end return contact end |
#path ⇒ Object
75 76 77 |
# File 'lib/ppl/adapter/storage/disk.rb', line 75 def path @directory.path end |
#save_contact(contact) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/ppl/adapter/storage/disk.rb', line 54 def save_contact(contact) vcard = @vcard_adapter.encode(contact) filename = filename_for_contact(contact) File.open(filename, "w") do |file| file.write(vcard) end end |