class Zugzwang::Connection
Public Class Methods
new(database_path, adapter)
click to toggle source
# File lib/zugzwang/connection/connection.rb, line 6 def initialize(database_path, adapter) @database_path = database_path @adapter = adapter case adapter when :sqlite then @database = Sequel.sqlite("#{database_path}.sqlite3") end end
Public Instance Methods
populate(items)
click to toggle source
# File lib/zugzwang/connection/populate.rb, line 8 def populate(items) return unless create() files = items.map{|i|File.file?(i) ? Dir[i] : Dir["#{i}/**/*.pgn"]}.flatten puts if files.map{|f|File.file? f}.any?{|f|f==true} files.each do |f| record = {} prev = '' # Progress bar lines = `sed -n '=' #{f} | wc -l`.to_i puts "\e[90mProcessing file:\e[0m \e[1m#{f}\e[0m" progress_bar = ProgressBar.create( :title => 'Progress', total: lines, progress_mark: "\e[1;35m#{?#}\e[0m", remainder_mark: ?., format: "%t: %p%% (Line: %c/%C) %B" ) # Processing file File.open(f,'r').each do |line| if prev =~ /\A\[.*\Z/ && line =~ /\A[\n\r].*\Z/ @database[:games].insert(**record) record = {} prev = line progress_bar.increment next end if line =~ /\A([\n\r]|[0-9]).*\Z/ prev = line progress_bar.increment next end subbed = line.gsub(%r{[\r\n\[\]\"]},'') field, value = subbed.split(' ', 2) field = field.to_sym if FIELDS[field] record[field] = case FIELDS[field][:type] when :string then value when :integer then value.to_i when :date then Date.parse(value) when :time then value end end prev = line progress_bar.increment end end case @adapter when :sqlite puts "\n\e[92mComplete\e[0m: Populated #{@adapter} database at \e[1m#{@database_path}.sqlite3\e[0m." end end
Private Instance Methods
create()
click to toggle source
# File lib/zugzwang/connection/populate.rb, line 67 def create begin @database.create_table :games do primary_key :id FIELDS.each do |name, options| case options[:type] when :string then String name, size: (options[:size]||255) when :integer then Integer name when :date then Date name when :time then Time name, only_time: true end end end return true rescue Sequel::DatabaseError pass = false until pass puts "\n\e[1;91mERROR\e[0m: '\e[1mgames\e[0m' table already exists in the database." response = Ask.new.msg("\e[1mPROMPT: \e[0mOverwrite table? (\e[1mEXISTING DATA WILL BE DELETED!\e[0m) [Y/n]") if %w[Y y YES Yes yes].include? response @database.create_table! :games do primary_key :id FIELDS.each do |name, options| case options[:type] when :string then String name, size: (options[:size]||255) when :integer then Integer name when :date then Date name when :time then Time name, only_time: true end end end pass = true return true elsif %w[N n NO No no].include? response pass = true return false end end end end