Module: Velibe::Database
- Defined in:
- lib/velibe/db/database.rb
Constant Summary collapse
- NAME =
TODO: more generic
'~/.velib.db'- PATH =
.to_s?
Pathname.new(NAME).
- DATA_CSV =
'../../data/Paris.csv'
Class Method Summary collapse
- .active_connect ⇒ Object
- .connexion ⇒ Object
- .create ⇒ Object
- .create! ⇒ Object
- .exist? ⇒ Boolean
- .make_schema ⇒ Object
- .populate ⇒ Object
-
.prune ⇒ Object
§todo: version block?.
Class Method Details
.active_connect ⇒ Object
21 22 23 24 |
# File 'lib/velibe/db/database.rb', line 21 def self.active_connect ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: PATH.to_s) end |
.connexion ⇒ Object
37 38 39 40 41 |
# File 'lib/velibe/db/database.rb', line 37 def self.connexion return SQlite3::Database.new(PATH.to_s) # §see:options # §maybe: delete? [not that working?] end |
.create ⇒ Object
26 27 28 29 30 |
# File 'lib/velibe/db/database.rb', line 26 def self.create active_connect make_schema populate end |
.create! ⇒ Object
32 33 34 35 |
# File 'lib/velibe/db/database.rb', line 32 def self.create! prune create end |
.exist? ⇒ Boolean
17 18 19 |
# File 'lib/velibe/db/database.rb', line 17 def self.exist? PATH.exist? # §check end |
.make_schema ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/velibe/db/database.rb', line 50 def self.make_schema ActiveRecord::Schema.define do create_table :stations do |t| t.integer :number t.string :name t.string :address t.float :latitude t.float :longitude t.index :number # ¤note: must be declared before end create_table :statuses do |t| t.integer :station_id t.boolean :status t.integer :bike_stands t.integer :available_bikes t.integer :available_bike_stands t. :last_update # t.timestamps end #§todo: create others end end |
.populate ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/velibe/db/database.rb', line 77 def self.populate # MAYBE: Use fast cv csv_file = File.join(File.dirname(File.(__FILE__)), DATA_CSV) # TODO: extract # ¤see: stopwatch > ??? puts 'Populate Database from csv Station description' # ¤note: transaction for faster insert ActiveRecord::Base.transaction do CSV.foreach(csv_file, headers: true, converters: :numeric) do |row| # §TODO: converter # header_converters: :underscore -> tried but get: NoMethodError: undefined method `arity' for nil:NilClass Models::Station.create(number: row['Number'], name: row['Name'], address: row['Address'], latitude: row['Latitude'], longitude: row['Longitude']) # ¤note: inspect send back a hash end end end |
.prune ⇒ Object
§todo: version block?
44 45 46 47 |
# File 'lib/velibe/db/database.rb', line 44 def self.prune #§later: check no connected? FileUtils.rm(PATH) if exist? end |