Class: Biblioteque::Database

Inherits:
Model
  • Object
show all
Defined in:
lib/biblioteque/database.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#created_at, #id, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

generate_id, #save_to_file, #to_hash

Constructor Details

#initialize(params = {}) ⇒ Database

Returns a new instance of Database.



24
25
26
27
28
29
30
31
# File 'lib/biblioteque/database.rb', line 24

def initialize(params = {})
	super
	@libraries = []
	@dbpath = params[:path]
	@loaded = false
	@library = nil
	@to_hash_params = [:id, :name, :updated_at, :created_at]
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/biblioteque/database.rb', line 3

def path
  @path
end

#updated_atObject

Returns the value of attribute updated_at.



3
4
5
# File 'lib/biblioteque/database.rb', line 3

def updated_at
  @updated_at
end

Class Method Details

.clone(path_source, path_destination) ⇒ Object



14
15
16
17
18
# File 'lib/biblioteque/database.rb', line 14

def self.clone(path_source, path_destination)
	raise "No" unless path_source || path_destination
	FileUtils.cp(path_source, path_destination)
	true
end

.create(name, path) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/biblioteque/database.rb', line 5

def self.create(name, path)
	raise "No" unless name || path
	new_db = {id:generate_id,name:name,created_at:Time.now,updated_at:Time.now}
	File.open(path,"w+") do |f|
	  f.write(new_db.to_json)
	end
	true
end

.destroy(path_source) ⇒ Object



20
21
22
# File 'lib/biblioteque/database.rb', line 20

def self.destroy(path_source)
	File.delete(path_source) ? true : false
end

Instance Method Details

#close_libraryObject



50
51
52
# File 'lib/biblioteque/database.rb', line 50

def close_library
	@library = nil
end

#create_library(name) ⇒ Object



33
34
35
36
37
38
# File 'lib/biblioteque/database.rb', line 33

def create_library(name)
	raise "No" unless name
	lib = Library.create(name, self.id)
	@libraries << lib
	lib
end

#current_libraryObject



54
55
56
# File 'lib/biblioteque/database.rb', line 54

def current_library
	@library
end

#delete_library(id) ⇒ Object



93
94
95
96
97
# File 'lib/biblioteque/database.rb', line 93

def delete_library(id)
	return false unless id.to_i
	libraries.delete_if{|l| l.id == id }
	true
end

#librariesObject



58
59
60
# File 'lib/biblioteque/database.rb', line 58

def libraries
	@libraries
end

#loadObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/biblioteque/database.rb', line 62

def load
	json = File.read(@dbpath)
	db = JSON.parse(json)
	self.id = db["id"]
	self.name = db["name"]
	self.path = @dbpath
	self.created_at = db["created_at"]
	self.updated_at = db["updated_at"]
	load_libraries(db["libraries"])
	@loaded = true
end

#load_library(library_id) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/biblioteque/database.rb', line 40

def load_library(library_id)
	@libraries.each do |lib|
		if lib.id == library_id
			@library = lib
			return lib
		end
	end	
	nil
end

#saveObject



78
79
80
81
82
83
# File 'lib/biblioteque/database.rb', line 78

def save
	db = self.to_hash
	db[:libraries] = []
	libraries.each{|l| db[:libraries] << l.to_hash }
	save_to_file(@dbpath ,db.to_json)
end

#search(filter) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/biblioteque/database.rb', line 85

def search(filter)
	results = []
	libraries.each do |l|
		results << {library:l.id,results:l.search(filter)}
	end
	results
end

#statusObject



74
75
76
# File 'lib/biblioteque/database.rb', line 74

def status
	@loaded ? "Loaded" : "Not Loaded"
end