Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add sqlite exporter

  • Loading branch information...
commit 46d2e01a4953b8f54e8625c9111cf5b372d80b49 1 parent 066bbca
@nullstyle authored
View
2  given_name/j/ja.yml
@@ -95,6 +95,8 @@
gender: male
- james:
gender: unisex
+ census1990:
+ frequency: 3.318
- jamey:
gender: unisex
- jami:
View
1  given_name/s/sc.yml
@@ -7,6 +7,7 @@
gender: male
- scott:
gender: unisex
+ origin: gaelic
- scottie:
gender: unisex
- scotty:
View
19 lib/name-database/entry.rb
@@ -20,6 +20,25 @@ def initialize(set, file, data)
end
end
+ def metadata_without_nesting
+ returning({}) do |result|
+ NameDatabase::Entry.flattened_meta("", result, meta)
+ end
+ end
+
+ def self.flattened_meta(prefix, result, meta)
+ meta.each do |key, value|
+ flattened_key = prefix.blank? ? key : "#{prefix}/#{key}"
+
+ case value
+ when Hash
+ result.merge! flattened_meta(flattened_key, result, value)
+ else
+ result[flattened_key] = value
+ end
+ end
+ end
+
def path()
set.data_file_path(name)
View
53 tasks/exporters/sqlite.rake
@@ -0,0 +1,53 @@
+require 'sequel'
+
+task "export:sqlite" => [:db, "export:sqlite:schema"] do
+
+ #first insert the given_names
+ @names = @output[:names]
+ @meta = @output[:metadata]
+
+ puts "dumping given names"
+ @db.given_names.each do |entry|
+ row = @names.insert :name => entry.name, :type => "given"
+
+ entry.metadata_without_nesting.each do |key, value|
+ @meta.insert :key => key, :value => value
+ end
+ end
+
+ # puts "dumping family names"
+ # @db.family_names.each do |entry|
+ # @names << {:name => entry.name, :type => "family"}
+ # end
+
+end
+
+namespace "export:sqlite" do
+ task :db => "out" do
+
+ existing = Dir["out/*.sqlite"].length
+ path = "out/names#{(".#{existing}" if existing > 0)}.sqlite"
+
+ @output = Sequel.sqlite(path)
+ end
+
+ task :schema => "export:sqlite:db" do
+
+ @output.create_table :names do
+ primary_key :id
+ String :name
+ String :type
+
+ index [:type, :name]
+ end
+
+ @output.create_table :metadata do
+ foreign_key :name_id, :names
+ String :key
+ String :value
+
+ index [:name_id, :key]
+ end
+
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.