Class: Currently::Filestore

Inherits:
Backingstore show all
Defined in:
lib/currently/filestore.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Filestore

Returns a new instance of Filestore.



6
7
8
9
# File 'lib/currently/filestore.rb', line 6

def initialize(filename)
  # raise "log file #{filename} doesn't exist!" unless File.exists? filename
  @filename = filename
end

Instance Method Details

#allObject

retrieves all entries (or as many as can be found)



17
18
19
# File 'lib/currently/filestore.rb', line 17

def all
  self.parse
end

#last(n = 1) ⇒ Object

retrieves last n entries



12
13
14
# File 'lib/currently/filestore.rb', line 12

def last(n = 1)
  self.parse.last n
end

#parseObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/currently/filestore.rb', line 28

def parse
  # sloooooooow
  entries = []
  return entries unless File.exists? @filename
  File.open(@filename) {|f|
    f.readlines.each{|l|
      chunks = l.split("|")
      ts = chunks[0]
      rest = chunks[1..-1].join("|").split(" ")
      contexts = []
      rest.reverse.each {|c|
        if c =~ /^\#.+/
          contexts << c[1..-1]
        else
          break
        end
      }
      contexts = contexts.reverse
      text = rest[0..-(contexts.length + 1)].join(" ")
      entries << Entry.new(text,contexts,ts)
    }
    entries
  }
end

#save(entry) ⇒ Object

saves the provided entry



22
23
24
25
26
# File 'lib/currently/filestore.rb', line 22

def save(entry)
  File.open(@filename,"a"){|f|
    f.puts entry.serialize_to_line
  }
end