Class: Mongoose::Database
Overview
Database class
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
Instance Method Summary collapse
-
#close ⇒ Object
—————————————————————————– close —————————————————————————–.
-
#create_table(table_name) {|Object.const_get(class_name)| ... } ⇒ Object
—————————————————————————– create_table —————————————————————————–.
-
#drop_table(table_name) ⇒ Object
—————————————————————————– drop_table —————————————————————————–.
-
#initialize(params = {}) ⇒ Database
constructor
—————————————————————————– initialize —————————————————————————–.
-
#table_exists?(table_name) ⇒ Boolean
—————————————————————————– table_exists? —————————————————————————–.
Constructor Details
#initialize(params = {}) ⇒ Database
initialize
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/mongoose/database.rb', line 12 def initialize(params={}) @path = params[:path] || './' Table.db = self @tables = {} Dir.foreach(@path) do |filename| next unless File.extname(filename) == TBL_HDR_EXT table_name = File.basename(filename, ".*").to_sym init_table(table_name) end end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/mongoose/database.rb', line 7 def path @path end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
7 8 9 |
# File 'lib/mongoose/database.rb', line 7 def tables @tables end |
Instance Method Details
#close ⇒ Object
close
30 31 32 |
# File 'lib/mongoose/database.rb', line 30 def close @tables.each_key { |tbl_class| tbl_class.close } end |
#create_table(table_name) {|Object.const_get(class_name)| ... } ⇒ Object
create_table
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mongoose/database.rb', line 37 def create_table(table_name) raise "Table already exists!" if table_exists?(table_name) class_name = Util.us_case_to_class_case(table_name) tbl_header = {} tbl_header[:table_name] = table_name tbl_header[:class_name] = class_name tbl_header[:last_id_used] = 0 tbl_header[:deleted_recs_counter] = 0 tbl_header[:columns] = [] tbl_header[:columns] << { :name => :id, :data_type => :integer, :class => IDColumn.to_s } File.open(File.join(@path, table_name.to_s + TBL_HDR_EXT), 'w') do |f| YAML.dump(tbl_header, f) end fptr = File.open(File.join(@path, table_name.to_s + TBL_EXT), 'w') fptr.close init_table(table_name) yield Object.const_get(class_name) if block_given? end |
#drop_table(table_name) ⇒ Object
drop_table
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mongoose/database.rb', line 66 def drop_table(table_name) class_name = Util.us_case_to_class_case(table_name) @tables[Object.const_get(class_name)][:columns].each do |c| if c.indexed? File.delete(c.index_file_name) if File.exists?(c.index_file_name) end end File.delete(File.join(@path, table_name.to_s + TBL_HDR_EXT)) File.delete(File.join(@path, table_name.to_s + TBL_EXT)) if \ File.exists?(File.join(@path, table_name.to_s + TBL_EXT)) @tables.delete(Object.const_get(class_name)) end |
#table_exists?(table_name) ⇒ Boolean
table_exists?
85 86 87 |
# File 'lib/mongoose/database.rb', line 85 def table_exists?(table_name) return File.exists?(File.join(@path, table_name.to_s + TBL_HDR_EXT)) end |