Class: WorkLogFileDao
- Inherits:
-
Object
- Object
- WorkLogFileDao
- Defined in:
- lib/work_log_file_dao.rb
Constant Summary collapse
- DB_FILE_NAME =
'myworklog.pstore'
- DB_FOLDER =
'.myworklog'
- HOME_FOLDER =
File.('~')
- FULL_DB_FILE_PATH =
"#{HOME_FOLDER}/#{DB_FOLDER}/#{DB_FILE_NAME}"
Instance Method Summary collapse
- #delete(id) ⇒ Object
- #find_by_month_and_year(month, year) ⇒ Object
- #find_by_year(year) ⇒ Object
-
#initialize ⇒ WorkLogFileDao
constructor
A new instance of WorkLogFileDao.
- #list(date) ⇒ Object
- #list_all ⇒ Object
- #save(work_log) ⇒ Object
Constructor Details
#initialize ⇒ WorkLogFileDao
Returns a new instance of WorkLogFileDao.
12 13 14 15 16 17 |
# File 'lib/work_log_file_dao.rb', line 12 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
#delete(id) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/work_log_file_dao.rb', line 40 def delete(id) PStore.new(FULL_DB_FILE_PATH).transaction do |store| raise ArgumentError, "Id #{id} not found" if store[id].nil? store.delete(id) end end |
#find_by_month_and_year(month, year) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/work_log_file_dao.rb', line 48 def find_by_month_and_year(month, year) store = PStore.new(FULL_DB_FILE_PATH) list = [] store.transaction(true) do store.roots.map do |root| if store[root].date.month == month && store[root].date.year == year list << store[root] end end end list end |
#find_by_year(year) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/work_log_file_dao.rb', line 61 def find_by_year(year) store = PStore.new(FULL_DB_FILE_PATH) list = [] store.transaction(true) do store.roots .map do |root| list << store[root] if store[root].date.year == year end end list end |
#list(date) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/work_log_file_dao.rb', line 23 def list(date) store = PStore.new(FULL_DB_FILE_PATH) list = [] store.transaction(true) do store.roots .map { |root| list << store[root] if store[root].date == date } end list end |
#list_all ⇒ Object
33 34 35 36 37 38 |
# File 'lib/work_log_file_dao.rb', line 33 def list_all store = PStore.new(FULL_DB_FILE_PATH) list = [] store.transaction(true) { store.roots.map { |root| list << store[root] } } list end |
#save(work_log) ⇒ Object
19 20 21 |
# File 'lib/work_log_file_dao.rb', line 19 def save(work_log) PStore.new(FULL_DB_FILE_PATH).transaction { |store| store[work_log.id] = work_log } end |