Class: Heathrow::AddressBook
- Inherits:
-
Object
- Object
- Heathrow::AddressBook
- Defined in:
- lib/heathrow/address_book.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
Instance Method Summary collapse
-
#complete(partial) ⇒ Object
Get completion candidates for a partial string.
-
#expand(name) ⇒ Object
Expand an alias name to full address, or return input unchanged.
-
#initialize(path = File.expand_path('~/setup/addressbook')) ⇒ AddressBook
constructor
A new instance of AddressBook.
-
#lookup(query) ⇒ Object
Search aliases and addresses by query string.
-
#names ⇒ Object
Get all alias names.
- #parse(path) ⇒ Object
Constructor Details
#initialize(path = File.expand_path('~/setup/addressbook')) ⇒ AddressBook
Returns a new instance of AddressBook.
5 6 7 8 |
# File 'lib/heathrow/address_book.rb', line 5 def initialize(path = File.('~/setup/addressbook')) @aliases = {} parse(path) if File.exist?(path) end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
3 4 5 |
# File 'lib/heathrow/address_book.rb', line 3 def aliases @aliases end |
Instance Method Details
#complete(partial) ⇒ Object
Get completion candidates for a partial string
34 35 36 37 38 39 40 |
# File 'lib/heathrow/address_book.rb', line 34 def complete(partial) return [] if partial.nil? || partial.empty? partial_down = partial.downcase @aliases.select { |k, v| k.start_with?(partial_down) || v.downcase.include?(partial_down) }.map { |k, v| "#{k} → #{v}" } end |
#expand(name) ⇒ Object
Expand an alias name to full address, or return input unchanged
24 25 26 |
# File 'lib/heathrow/address_book.rb', line 24 def (name) @aliases[name] || name end |
#lookup(query) ⇒ Object
Search aliases and addresses by query string
18 19 20 21 |
# File 'lib/heathrow/address_book.rb', line 18 def lookup(query) query_down = query.downcase @aliases.select { |k, v| k.include?(query_down) || v.downcase.include?(query_down) } end |
#names ⇒ Object
Get all alias names
29 30 31 |
# File 'lib/heathrow/address_book.rb', line 29 def names @aliases.keys.sort end |
#parse(path) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/heathrow/address_book.rb', line 10 def parse(path) File.readlines(path).each do |line| next unless line =~ /^alias\s+(\S+)\s+(.+)/ @aliases[$1] = $2.strip end end |