Module: Highland::DatabaseMethods
- Included in:
- Highland
- Defined in:
- lib/highland/database_methods.rb
Overview
DatabaseMethods are not the part of the user API. These methods are the fundamentals of the one. They work with static hashes and manage virtual ones. It’s not recommended to work with these methods directly.
Instance Method Summary collapse
-
#clear_static ⇒ Object
Clears static hash (collection).
-
#clear_virtual ⇒ Object
Clears virtual hash and virtual helper.
-
#delete(*query) ⇒ Object
Deletes element from collection.
-
#find_db(*query) ⇒ Object
Finds hash elements according to the query.
-
#generate_hl ⇒ Object
Creates static hash in the database folder.
-
#init_file(shash) ⇒ Object
Defines @file instance variable.
-
#insert_shash(element) ⇒ Object
Inserts hash element into the static hash.
-
#insert_vhash(element) ⇒ Object
Inserts hash element into the virtual hash.
-
#insert_vhelper(element) ⇒ Object
Inserts hash element into the virtual helper.
-
#load_vhash(shash) ⇒ Object
Loads virtual hash.
-
#load_vhelper ⇒ Object
Loads virtual helper.
-
#reload_virtual ⇒ Object
Reloads virtual hash and virtual helper.
-
#return_vhash ⇒ Object
Returns virtual hash.
-
#return_vhelper ⇒ Object
Returns virtual helper.
-
#update_columns ⇒ Object
Assigns the latest information about columns the @columns.
-
#update_db(*query) ⇒ Object
Updates element.
Instance Method Details
#clear_static ⇒ Object
Clears static hash (collection).
129 130 131 |
# File 'lib/highland/database_methods.rb', line 129 def clear_static File.truncate(@file, 0) end |
#clear_virtual ⇒ Object
Clears virtual hash and virtual helper.
115 116 117 |
# File 'lib/highland/database_methods.rb', line 115 def clear_virtual @vhash, @vhelper = {}, {} end |
#delete(*query) ⇒ Object
Deletes element from collection.
135 136 137 138 139 140 141 142 143 |
# File 'lib/highland/database_methods.rb', line 135 def delete(*query) find_db(*query).each_key do |id| @vhash.remove!(id) end load_vhelper clear_static insert_shash(@vhash) return true end |
#find_db(*query) ⇒ Object
Finds hash elements according to the query.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/highland/database_methods.rb', line 79 def find_db(*query) if query[0].class == Fixnum id = query[0] output = {} output[id] = @vhash[id] if @vhash[id] != nil return output end if query[0].class == Hash q = query[0] ids = [] q.each_key do |column| ids << @vhelper[column.to_s][q[column]] end res = ids.inject(ids.first){ |sim,cur| sim & cur } return {} if res.class != Array or res == [] output = {} res.each do |id| next if @vhash[id] == nil output[id] = @vhash[id] end return output end end |
#generate_hl ⇒ Object
Creates static hash in the database folder.
22 23 24 |
# File 'lib/highland/database_methods.rb', line 22 def generate_hl File.new(@file, "w") end |
#init_file(shash) ⇒ Object
Defines @file instance variable.
15 16 17 18 |
# File 'lib/highland/database_methods.rb', line 15 def init_file(shash) @file = shash generate_hl if File.exists?(@file) == false end |
#insert_shash(element) ⇒ Object
Inserts hash element into the static hash.
105 106 107 108 109 110 111 |
# File 'lib/highland/database_methods.rb', line 105 def insert_shash(element) File.open(@file, 'a') do |file| element.each_key do |id| file.puts "#{id} => #{element[id]}," end end end |
#insert_vhash(element) ⇒ Object
Inserts hash element into the virtual hash.
57 58 59 60 61 |
# File 'lib/highland/database_methods.rb', line 57 def insert_vhash(element) element.each_key do |key| @vhash[key] = element[key] end end |
#insert_vhelper(element) ⇒ Object
Inserts hash element into the virtual helper.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/highland/database_methods.rb', line 65 def insert_vhelper(element) element.each_key do |id| element[id].each_key do |column| @vhelper[column] ||= {} item = element[id][column]["value"] @vhelper[column][item] ||= [] @vhelper[column][item] << id end end update_columns end |
#load_vhash(shash) ⇒ Object
Loads virtual hash.
28 29 30 31 32 |
# File 'lib/highland/database_methods.rb', line 28 def load_vhash(shash) init_file(shash) data = "{#{rio(@file).contents}}" @vhash = eval(data) end |
#load_vhelper ⇒ Object
Loads virtual helper.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/highland/database_methods.rb', line 42 def load_vhelper @vhelper = {} @vhash.each_key do |id| @vhash[id].each_key do |column| @vhelper[column] ||= {} item = @vhash[id][column]["value"] @vhelper[column][item] ||= [] @vhelper[column][item] << id end end update_columns end |
#reload_virtual ⇒ Object
Reloads virtual hash and virtual helper.
121 122 123 124 125 |
# File 'lib/highland/database_methods.rb', line 121 def reload_virtual clear_virtual load_vhash(@file) load_vhelper end |
#return_vhash ⇒ Object
Returns virtual hash.
161 162 163 |
# File 'lib/highland/database_methods.rb', line 161 def return_vhash @vhash end |
#return_vhelper ⇒ Object
Returns virtual helper.
167 168 169 |
# File 'lib/highland/database_methods.rb', line 167 def return_vhelper @vhelper end |
#update_columns ⇒ Object
Assigns the latest information about columns the @columns.
36 37 38 |
# File 'lib/highland/database_methods.rb', line 36 def update_columns @columns = @vhelper.keys end |
#update_db(*query) ⇒ Object
Updates element.
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/highland/database_methods.rb', line 147 def update_db(*query) id = query[0][:id] query[0].each_key do |key| next if key == :id @vhash[id][key.to_s]["value"] = query[0][key] if @vhash[id].has_key?(key.to_s) end load_vhelper clear_static insert_shash(@vhash) return true end |