Class: Heathrow::AddressBook

Inherits:
Object
  • Object
show all
Defined in:
lib/heathrow/address_book.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.expand_path('~/setup/addressbook'))
  @aliases = {}
  parse(path) if File.exist?(path)
end

Instance Attribute Details

#aliasesObject (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 expand(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

#namesObject

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