Class: LideoDao

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

Constant Summary collapse

DB_FILE_NAME =
'lideo.pstore'
DB_FOLDER =
'.lideo'
HOME_FOLDER =
File.expand_path('~')
FULL_DB_FILE_PATH =
"#{HOME_FOLDER}/#{DB_FOLDER}/#{DB_FILE_NAME}"

Instance Method Summary collapse

Constructor Details

#initializeLideoDao

Returns a new instance of LideoDao.



9
10
11
12
13
14
# File 'lib/lideo_dao.rb', line 9

def initialize
  db_folder_full_path = "#{HOME_FOLDER}/#{DB_FOLDER}"
  unless File.directory?(db_folder_full_path)
    FileUtils.mkdir_p(db_folder_full_path)
  end
end

Instance Method Details

#allObject



30
31
32
33
34
35
36
37
# File 'lib/lideo_dao.rb', line 30

def all
  store = PStore.new(FULL_DB_FILE_PATH)
  list = []
  store.transaction(true) do
    store.roots.map { |root| list << store[root] }
  end
  list
end

#delete_feed(url) ⇒ Object



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

def delete_feed(url)
  PStore.new(FULL_DB_FILE_PATH).transaction do |store|
    raise ArgumentError, "Feed #{url} not found" if store[url].nil?

    store.delete(url)
  end
end

#find(group) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/lideo_dao.rb', line 20

def find(group)
  store = PStore.new(FULL_DB_FILE_PATH)
  list = []
  store.transaction(true) do
    store.roots
      .map { |root| list << store[root] if store[root].group == group }
  end
  list
end

#save(feed) ⇒ Object



16
17
18
# File 'lib/lideo_dao.rb', line 16

def save(feed)
  PStore.new(FULL_DB_FILE_PATH).transaction { |store| store[feed.url] = feed }
end