Class: Logbook::Book

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ Book

Returns a new instance of Book.



12
13
14
15
# File 'lib/logbook/book.rb', line 12

def initialize(id=nil)
  @id = id
  @store = Logbook.store
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/logbook/book.rb', line 8

def id
  @id
end

#storeObject

Returns the value of attribute store.



9
10
11
# File 'lib/logbook/book.rb', line 9

def store
  @store
end

Instance Method Details

#add(time, text) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/logbook/book.rb', line 39

def add(time, text)
  store_available
  must_exist

  page = get(time)
  store.update(id, time, "#{page ? "#{page}\n" : ""}#{text}")
  time
end

#add_temporal(text) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/logbook/book.rb', line 24

def add_temporal(text)
  temporal = false

  time = nil
  if text =~ /(.+?)\s*:\s*(.*)/
    time = Chronic.parse($1)
    text = $2 if time
    temporal = true if time
  end

  time ||= Time.now

  add time, text
end

#allObject



56
57
58
59
60
61
# File 'lib/logbook/book.rb', line 56

def all
  store_available
  must_exist
  
  store.all(id)
end

#create(covertext = nil) ⇒ Object



17
18
19
20
21
22
# File 'lib/logbook/book.rb', line 17

def create(covertext=nil)
  store_available

  cover = covertext || "#{ENV['USER'] || 'Captain'}'s log."
  @id = store.create cover
end

#destroyObject



63
64
65
66
67
68
# File 'lib/logbook/book.rb', line 63

def destroy
  store_available
  must_exist

  store.destroy(id)
end

#get(time) ⇒ Object



48
49
50
51
52
53
# File 'lib/logbook/book.rb', line 48

def get(time)
  store_available
  must_exist

  page = store.get(id, time)
end

#must_existObject



74
75
76
# File 'lib/logbook/book.rb', line 74

def must_exist
  raise "create a book first" unless @id
end

#store_availableObject



70
71
72
# File 'lib/logbook/book.rb', line 70

def store_available
  raise store.error unless store.valid?
end