Class: ContactList
- Inherits:
-
Object
- Object
- ContactList
- Defined in:
- lib/public/doc/plain-text-files/contacts_g.rb,
lib/public/doc/plain-text-files/contacts_y.rb
Instance Attribute Summary collapse
-
#contact_cache ⇒ Object
readonly
Returns the value of attribute contact_cache.
-
#contacts ⇒ Object
Returns the value of attribute contacts.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(contact) ⇒ Object
- #[](name) ⇒ Object
- #delete(name) ⇒ Object
- #directory_names ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(file) ⇒ ContactList
constructor
A new instance of ContactList.
- #populate_contact(contact) ⇒ Object
- #save ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(file) ⇒ ContactList
Returns a new instance of ContactList.
8 9 10 11 |
# File 'lib/public/doc/plain-text-files/contacts_g.rb', line 8 def initialize(dir) @dir = dir @contact_cache = [] end |
Instance Attribute Details
#contact_cache ⇒ Object (readonly)
Returns the value of attribute contact_cache.
6 7 8 |
# File 'lib/public/doc/plain-text-files/contacts_g.rb', line 6 def contact_cache @contact_cache end |
#contacts ⇒ Object
Returns the value of attribute contacts.
4 5 6 |
# File 'lib/public/doc/plain-text-files/contacts_y.rb', line 4 def contacts @contacts end |
Class Method Details
.load(file) ⇒ Object
37 38 39 40 41 |
# File 'lib/public/doc/plain-text-files/contacts_y.rb', line 37 def self.load(file) list = new(file) list.contacts = YAML.load(File.read(file)) list end |
Instance Method Details
#<<(contact) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/public/doc/plain-text-files/contacts_g.rb', line 34 def <<(contact) Dir.chdir(@dir) do Dir.mkdir(contact.dirname) unless File.exists?(contact.dirname) populate_contact(contact) end @contact_cache << contact end |
#[](name) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/public/doc/plain-text-files/contacts_g.rb', line 13 def [](name) contact = @contact_cache.find {|c| c.name == name } return contact if contact contact = Contact.new(name) Dir.chdir(@dir) do if File.directory?(contact.dirname) populate_contact(contact) @contact_cache << contact else contact = nil end end contact end |
#delete(name) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/public/doc/plain-text-files/contacts_g.rb', line 42 def delete(name) contact = self[name] return false unless contact contact.close Dir.chdir(@dir) do FileUtils.rm_rf(contact.dirname) end contact_cache.delete_if {|c| c.name == name } true end |
#directory_names ⇒ Object
53 54 55 |
# File 'lib/public/doc/plain-text-files/contacts_g.rb', line 53 def directory_names Dir["#{@dir}/*"] end |
#empty? ⇒ Boolean
61 62 63 |
# File 'lib/public/doc/plain-text-files/contacts_g.rb', line 61 def empty? directory_names.empty? end |
#populate_contact(contact) ⇒ Object
28 29 30 31 32 |
# File 'lib/public/doc/plain-text-files/contacts_g.rb', line 28 def populate_contact(contact) Dir.chdir(contact.dirname) do contact.open end end |
#save ⇒ Object
31 32 33 34 35 |
# File 'lib/public/doc/plain-text-files/contacts_y.rb', line 31 def save File.open(@file, "w") do |fh| fh.puts(@contacts.to_yaml) end end |
#size ⇒ Object
57 58 59 |
# File 'lib/public/doc/plain-text-files/contacts_g.rb', line 57 def size directory_names.size end |