/dao/dao_factory.rb
Ruby | 133 lines | 92 code | 18 blank | 23 comment | 6 complexity | cb26e8c356594fe4c830bda2b2326601 MD5 | raw file
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, version 3 of the License.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- #
- # Copyright: (C) 2004-2010 Rafal Kotusiewicz aka 'jjhop'. All rights reserved.
- #
- # http://bitbucket.org/jjhop/mycash/overview/
- # http://bitbucket.org/jjhop/mycash/wiki/Home
- #
-
- require 'rubygems'
- require 'singleton'
-
- require 'sqlite3'
-
- require File.join(File.dirname(__FILE__), 'suspence_dao')
- require File.join(File.dirname(__FILE__), 'account_dao')
-
- module MyCashDAO
-
- # Ogólna klasa wyjštków zwišzanych z trwa?oœciš
- class DAOException < Exception; end
-
- # Fabryka obiektów DAO
- class Factory
- include Singleton
-
- def init_with_dbfile(dbfile)
- @dbh = SQLite3::Database.new(dbfile)
- @dbh.results_as_hash = true
-
- @suspence_dao = SuspenceDAO.new(@dbh)
- @account_dao = AccountDAO.new(@dbh)
- #@product_category_dao = ProductCategoryDAO.new(nil)
- #@product_dao = ProductDAO.new(nil)
- end
-
- def disconnet_from_database
- unless @dbh.closed?
- @dbh.close
- @suspence_dao = nil
- @account_dao = nil
- end
- end
-
- def suspence_dao
- unless @suspence_dao.nil?
- if block_given?
- yield @suspence_dao
- else
- @suspence_dao
- end
- else
- raise DAOException, 'Brak polaczenia z baza danych'
- end
- end
-
- def account_dao
- unless @account_dao.nil?
- if block_given?
- yield @account_dao
- else
- @account_dao
- end
- else
- raise DAOException, 'Brak polaczenia z baza danych'
- end
- end
-
- def product_category_dao
- unless @product_category_dao.nil?
- if block_given?
- yield @product_category_dao
- else
- @product_category_dao
- end
- else
- raise DAOException, 'Brak polaczenia z baza danych'
- end
- end
-
- def product_dao
- unless @product_dao.nil?
- if block_given?
- yield @product_dao
- else
- @product_dao
- end
- else
- raise DAOException, 'Brak polaczenia z baza danych'
- end
- end
-
- end
- end
-
- # TODO: testowanie przenieœ? do klasy testów
- # ma?y kodzik do testowania
- if __FILE__ == $0
-
- factory = MyCashDAO::Factory.instance
- factory.init_with_dbfile('../.db/test.db')
-
- factory.suspence_dao do |dao|
- dao.save_or_update(MyCashModel::Suspence.new({
- :suspence_id => 2, :created_at => nil,
- :amount => 20, :what => 22 }))
- s1 = dao.find_by_id(1)
- unless s1.nil?
- puts "id= #{s1.suspence_id}, created_at= #{s1.created_at}, :amount= #{s1.amount}, :what= #{s1.what}"
- end
- end
-
- factory.account_dao do |dao|
- dao.save_or_update(MyCashModel::Account.new({:created_at => nil,:name => 'millenium'}))
- s1 = dao.find_by_id(1)
- unless s1.nil?
- puts "id= #{s1.account_id}, created_at= #{s1.created_at}, :name= #{s1.name}"
- end
- dao.delete_by_id(4)
- dao.get_all.each do |a|
- puts "id= #{a.account_id}, created_at= #{a.created_at}, :name= #{a.name}"
- end
- end
- end