Sha256: 9202f2758fe2b88a2c33db4b98720e8bceb7db414a7048af5392c7308f1aec5c

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

require 'libsql'

module ::Libsql
  class Iso3166Database < Database
    def self.country_data_file
      @country_data_file ||= File.expand_path( File.join( File.dirname(__FILE__), "data", "iso-3166-country.txt" ) )
    end

    def self.subcountry_data_file
      @subcountry_data_file ||= File.expand_path( File.join( File.dirname(__FILE__), "data", "iso-3166-subcountry.txt" ) )
    end

    def self.schema_file
      @schema_file ||= File.expand_path(File.join(File.dirname(__FILE__), "data", "iso-3166-schema.sql"))
    end

    def self.default_db_file
      @db_file ||= File.expand_path(File.join(File.dirname(__FILE__), "data", "iso-3166.db"))

    end

    def self.memory
      Iso3166Database.new( ":memory:" )
    end

    def initialize( path = Iso3166Database.default_db_file )
      @path = path 
      super( @path )
      install_schema( self )
      populate( self )
    end

    def duplicate( slug )
      dirname = File.dirname( @path )
      bname = File.basename( @path, ".db" )
      new_name = File.join( dirname, "#{bname}_#{slug}.db" )
      File.unlink( new_name ) if File.exist?( new_name )
      new_db = replicate_to( new_name )
      new_db.close
      return new_name
    end

    def install_schema( db )
      db.execute_batch( IO.read( Iso3166Database.schema_file ) );
    end

    def populate( db )
      db.import_csv_to_table( Iso3166Database.country_data_file, "country", :col_sep => "|" )
      db.import_csv_to_table( Iso3166Database.subcountry_data_file, "subcountry", :col_sep => "|" )
    end

    def remove
      File.unlink( @path ) if File.exist?( @path )
    end
  end
end


Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
libsql-0.1.0-x64-mingw-ucrt spec/iso_3166_database.rb
libsql-0.1.0-x64-mingw32 spec/iso_3166_database.rb
libsql-0.1.0-x86-mingw32 spec/iso_3166_database.rb
libsql-0.1.0 spec/iso_3166_database.rb