PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/dao/dao_factory.rb

https://bitbucket.org/jjhop/mycash
Ruby | 133 lines | 92 code | 18 blank | 23 comment | 6 complexity | cb26e8c356594fe4c830bda2b2326601 MD5 | raw file
  1. # This program is free software: you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation, version 3 of the License.
  4. #
  5. # This program is distributed in the hope that it will be useful,
  6. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. # GNU General Public License for more details.
  9. #
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. #
  13. # Copyright: (C) 2004-2010 Rafal Kotusiewicz aka 'jjhop'. All rights reserved.
  14. #
  15. # http://bitbucket.org/jjhop/mycash/overview/
  16. # http://bitbucket.org/jjhop/mycash/wiki/Home
  17. #
  18. require 'rubygems'
  19. require 'singleton'
  20. require 'sqlite3'
  21. require File.join(File.dirname(__FILE__), 'suspence_dao')
  22. require File.join(File.dirname(__FILE__), 'account_dao')
  23. module MyCashDAO
  24. # Ogólna klasa wyjštków zwišzanych z trwa?o&#x153;ciš
  25. class DAOException < Exception; end
  26. # Fabryka obiektów DAO
  27. class Factory
  28. include Singleton
  29. def init_with_dbfile(dbfile)
  30. @dbh = SQLite3::Database.new(dbfile)
  31. @dbh.results_as_hash = true
  32. @suspence_dao = SuspenceDAO.new(@dbh)
  33. @account_dao = AccountDAO.new(@dbh)
  34. #@product_category_dao = ProductCategoryDAO.new(nil)
  35. #@product_dao = ProductDAO.new(nil)
  36. end
  37. def disconnet_from_database
  38. unless @dbh.closed?
  39. @dbh.close
  40. @suspence_dao = nil
  41. @account_dao = nil
  42. end
  43. end
  44. def suspence_dao
  45. unless @suspence_dao.nil?
  46. if block_given?
  47. yield @suspence_dao
  48. else
  49. @suspence_dao
  50. end
  51. else
  52. raise DAOException, 'Brak polaczenia z baza danych'
  53. end
  54. end
  55. def account_dao
  56. unless @account_dao.nil?
  57. if block_given?
  58. yield @account_dao
  59. else
  60. @account_dao
  61. end
  62. else
  63. raise DAOException, 'Brak polaczenia z baza danych'
  64. end
  65. end
  66. def product_category_dao
  67. unless @product_category_dao.nil?
  68. if block_given?
  69. yield @product_category_dao
  70. else
  71. @product_category_dao
  72. end
  73. else
  74. raise DAOException, 'Brak polaczenia z baza danych'
  75. end
  76. end
  77. def product_dao
  78. unless @product_dao.nil?
  79. if block_given?
  80. yield @product_dao
  81. else
  82. @product_dao
  83. end
  84. else
  85. raise DAOException, 'Brak polaczenia z baza danych'
  86. end
  87. end
  88. end
  89. end
  90. # TODO: testowanie przenie&#x153;? do klasy testów
  91. # ma?y kodzik do testowania
  92. if __FILE__ == $0
  93. factory = MyCashDAO::Factory.instance
  94. factory.init_with_dbfile('../.db/test.db')
  95. factory.suspence_dao do |dao|
  96. dao.save_or_update(MyCashModel::Suspence.new({
  97. :suspence_id => 2, :created_at => nil,
  98. :amount => 20, :what => 22 }))
  99. s1 = dao.find_by_id(1)
  100. unless s1.nil?
  101. puts "id= #{s1.suspence_id}, created_at= #{s1.created_at}, :amount= #{s1.amount}, :what= #{s1.what}"
  102. end
  103. end
  104. factory.account_dao do |dao|
  105. dao.save_or_update(MyCashModel::Account.new({:created_at => nil,:name => 'millenium'}))
  106. s1 = dao.find_by_id(1)
  107. unless s1.nil?
  108. puts "id= #{s1.account_id}, created_at= #{s1.created_at}, :name= #{s1.name}"
  109. end
  110. dao.delete_by_id(4)
  111. dao.get_all.each do |a|
  112. puts "id= #{a.account_id}, created_at= #{a.created_at}, :name= #{a.name}"
  113. end
  114. end
  115. end