Class: Model

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
app/core/Model.rb

Overview

Classe permettant de gérer la base de données sqlite3

Direct Known Subclasses

Configuration, Grille, Jeu, Score, Utilisateur

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModel

Initialisation



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/core/Model.rb', line 21

def initialize
	## Retrieving configuration from yaml files
	@conf = Conf.instance()
	
	@app    = @conf.app
	@dbConf = @conf.db
	@@db     = nil

	dbPath = Core::ROOT + Core::DEFAULT_DATABASE_DIR

	database = dbPath + @dbConf["development"]["database"]

	self.mkDatabaseDir(dbPath)
	self.connection(database)
	
end

Instance Attribute Details

#dbObject

Returns the value of attribute db



16
17
18
# File 'app/core/Model.rb', line 16

def db
  @db
end

Class Method Details

.inherited(subclass) ⇒ Object

Invoke methods when inherited

Parameters:

  • subclass

    The subclass

Returns:

  • Itself



91
92
93
94
95
96
# File 'app/core/Model.rb', line 91

def self.inherited(subclass)
	self.instance()
	super

	return self
end

Instance Method Details

#connection(database) ⇒ Object

Connect to a database

Parameters:

  • database

    The database

Returns:

  • Itself



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/core/Model.rb', line 65

def connection(database)
	## Create a SQLite database 
	if !File.exist?(database)
		if Core::DEBUG
			puts "SQLite3 database not found, it will be created."
		end
		begin 
			SQLite3::Database.new(database)
		rescue SQLite3::Exception => e
			puts "SQLite3 couldn't create a new database."
		end
	end
	
	## Connect to created dtabase
	@@db = SQLite3::Database.open(database)

	return self
end

#insert(**options) ⇒ Object

Insert datas in database

Parameters:

  • options

    Hash with columns and values

Returns:

  • Itself



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/core/Model.rb', line 105

def insert(**options)
	colsName = Array.new 
	values   = Array.new
	bonds    = Array.new

	## Parse options to get columns and values
	options.each do |index, value|
		colsName << index
		values << value
		bonds << "?"
	end

	colsName = colsName.join(",")
	bonds = bonds.join(",")

	if Core::DEBUG
		puts "Request: INSERT INTO " + self.class.to_s.downcase + " (#{colsName}) VALUES (#{bonds});"
		
		print "Values was: "
		options.each do |index, value|
			print "#{index} => #{value} | "
		end

		puts
		puts
	end

	@@db.execute "INSERT INTO " + self.class.to_s.downcase + " (#{colsName}) 
			VALUES (#{bonds});", values

	return self
end

#mkDatabaseDir(dbPath) ⇒ Object

Create a new database directory

Parameters:

  • dbPath

    The database path

Returns:

  • Itself



45
46
47
48
49
50
51
52
53
54
55
# File 'app/core/Model.rb', line 45

def mkDatabaseDir(dbPath)
	## Create a new database directory if not existing
	if !Dir.exist?(dbPath)
		if Core::DEBUG
			puts "Database directory not found, it will be created."
		end
		Dir.mkdir(dbPath)
	end

	return self
end

#to_h(req) ⇒ Object

Convert an array reques to a hash with columns => values

Parameters:

  • req

    The request

Returns:

  • Request hashed



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/core/Model.rb', line 145

def to_h(req)
	response = Array.new()

	## Transforme la réponse en hash
	(1...req.length).each do |row|
		response[row-1] = Hash.new()
		(0...req[0].length).each do |col|
			response[row-1][req[0][col]] = req[row][col]
		end
	end

	return response
end