Class: Timelog::Book::Projects

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

Instance Method Summary collapse

Constructor Details

#initialize(book) ⇒ Projects

Returns a new instance of Projects.



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

def initialize(book)
  @book = book
end

Instance Method Details

#add(client, project, shortcut = nil) ⇒ Object

Raises:

  • (ProjectExistsError)


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

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

#exists?(client, project) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ClientNotFoundError)


15
16
17
18
# File 'lib/timelog/book/projects.rb', line 15

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

#id(client, project) ⇒ Object

Raises:

  • (ProjectNotFoundError)


26
27
28
29
30
# File 'lib/timelog/book/projects.rb', line 26

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

#name(client, project) ⇒ Object

Raises:

  • (ProjectNotFoundError)


20
21
22
23
24
# File 'lib/timelog/book/projects.rb', line 20

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