Module: Velibe::Database
- Defined in:
- lib/velibe/db/database.rb
Constant Summary collapse
- DB_NAME =
TODO: more generic + config
'~/.velib.db'- DB_PATH =
.to_s?
Pathname.new(DB_NAME).
- DATA_CSV =
'../../../data/Paris.csv'- DATA_CSV_FILE =
File.join(File.dirname(File.(__FILE__)), DATA_CSV)
Class Method Summary collapse
- .active_connect ⇒ Object
- .connexion ⇒ Object
- .create ⇒ Object
- .create! ⇒ Object
- .exist? ⇒ Boolean
- .make_schema ⇒ Object
- .populate ⇒ Object
- .prune ⇒ Object
Class Method Details
.active_connect ⇒ Object
23 24 25 |
# File 'lib/velibe/db/database.rb', line 23 def self.active_connect ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: DB_PATH.to_s end |
.connexion ⇒ Object
38 39 40 41 42 |
# File 'lib/velibe/db/database.rb', line 38 def self.connexion return SQlite3::Database.new(DB_PATH.to_s) # §see:options # §maybe: delete? [not that working?] end |
.create ⇒ Object
27 28 29 30 31 |
# File 'lib/velibe/db/database.rb', line 27 def self.create active_connect make_schema populate end |
.create! ⇒ Object
33 34 35 36 |
# File 'lib/velibe/db/database.rb', line 33 def self.create! prune create end |
.exist? ⇒ Boolean
19 20 21 |
# File 'lib/velibe/db/database.rb', line 19 def self.exist? DB_PATH.exist? # §check end |
.make_schema ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/velibe/db/database.rb', line 49 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 end # §todo: create others end end |
.populate ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/velibe/db/database.rb', line 75 def self.populate # MAYBE: Use fast cv # ¤see: stopwatch > to bench creation. >> (but overkill since just one step operation) puts 'Populate Database from csv Station description' # ¤note: transaction for faster insert ActiveRecord::Base.transaction do CSV.foreach(DATA_CSV_FILE, headers: true, converters: :numeric) do |row| # §TODO: converter # header_converters: :underscore -> tried but get: NoMethodError: undefined method `arity' for nil:NilClass Station.create(number: row['Number'], name: row['Name'], address: row['Address'], latitude: row['Latitude'], longitude: row['Longitude']) end end end |
.prune ⇒ Object
44 45 46 |
# File 'lib/velibe/db/database.rb', line 44 def self.prune FileUtils.rm(DB_PATH) if self.exist? end |