Class: Timelog::Book::Clients

Inherits:
Object
  • Object
show all
Defined in:
lib/timelog/book/clients.rb

Instance Method Summary collapse

Constructor Details

#initialize(book) ⇒ Clients

Returns a new instance of Clients.



4
5
6
# File 'lib/timelog/book/clients.rb', line 4

def initialize(book)
  @book = book
end

Instance Method Details

#add(name, shortcut = nil) ⇒ Object

Raises:

  • (ClientExistsError)


8
9
10
11
12
# File 'lib/timelog/book/clients.rb', line 8

def add(name, shortcut = nil)
  raise ClientExistsError if exists? name or (shortcut and exists? shortcut)
  @book.database.execute("insert into clients (name, shortcut) values(?, ?)", [name, shortcut])
  @book.database.first_value("select id from clients order by id desc limit 1")
end

#exists?(client) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/timelog/book/clients.rb', line 14

def exists?(client)
  @book.database.first_value("select id from clients where id = ? or name = ? or shortcut = ?", [client, client, client])
end

#id(client) ⇒ Object

Raises:

  • (ClientNotFoundError)


23
24
25
26
# File 'lib/timelog/book/clients.rb', line 23

def id(client)
  raise ClientNotFoundError unless exists? client
  @book.database.first_value("select id from clients where id = ? or name = ? or shortcut = ?", [client, client, client])
end

#name(client) ⇒ Object

Raises:

  • (ClientNotFoundError)


18
19
20
21
# File 'lib/timelog/book/clients.rb', line 18

def name(client)
  raise ClientNotFoundError unless exists? client
  @book.database.first_value("select name from clients where id = ? or name = ? or shortcut = ?", [client, client, client])
end