ctoD 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e659f7965bf040105156fef5a58e1ba45cbe0b67
4
- data.tar.gz: 4fb16794c736fc8861e8447abf4fd532d95ed28b
3
+ metadata.gz: 9d90f00b6c38e9b924e9ed7aecbc4ae4989bf92c
4
+ data.tar.gz: c0a1d1078c31fb95ff07c5b5cdacbb665616e911
5
5
  SHA512:
6
- metadata.gz: ba3bb7e0ecf2990306b8e63c671eecdb8d6a6e311f01ef6698f8cacc59486b8d8e1475ae50f34ba40841f282a4624c376d15e550a96d551b395694b30b1dfae0
7
- data.tar.gz: b571ad050e1a6288fe93891ff83aae43ce791e301522c6708a4e37f416dcc0451483b01efcbe65c128dd0fe977c67853f025801945d553ca5e72195f793785c4
6
+ metadata.gz: 4a64d725eebc79102629e5d45643f2fa62271b58145ff6989ffefccee0c31f65a696202939950d21f8da2e79eac8135bd92bc220967df25ef5c8bced23f1da79
7
+ data.tar.gz: e90972194c5912518b4480de0919bff0de2de62751b82c37086d322faf3538b53249bf3b5c93ea14b861c45205f93bff08d3b734ff533bf62ef3d4e17c38a027
data/README.md CHANGED
@@ -24,8 +24,10 @@ It comes with `ctoD` command.
24
24
  % ctoD
25
25
  Commands:
26
26
  ctoD create_table CSV DATABASE # Create a database table for CSV
27
- ctoD export CSV DATABASE # Export CSV data to a DATABASE
27
+ ctoD export CSV DATABASE # Export CSV data to DATABASE
28
28
  ctoD help [COMMAND] # Describe available commands or one specific command
29
+ ctoD table_columns CSV # Show column name and type pairs for a table based on given CSV
30
+ ctoD version # Show CtoD version
29
31
 
30
32
  # Export movies.csv data to a table named 'movies' at postgres movies database.
31
33
  % ctoD export movies.csv postgres://localhost/movies
@@ -33,6 +35,9 @@ It comes with `ctoD` command.
33
35
  # Export movies.csv data to movies.sqlite3 database file.
34
36
  % ctoD export movies.csv sqlite3://localhost/${HOME}/.db/movies.sqlite3
35
37
 
38
+ # Check table column schema to be created based on given CSV.
39
+ % ctoD table_columns movies.csv
40
+
36
41
  ## Contributing
37
42
 
38
43
  1. Fork it
@@ -1,4 +1,5 @@
1
1
  require 'thor'
2
+ require 'csv'
2
3
 
3
4
  module CtoD
4
5
  class CLI < Thor
@@ -23,6 +24,16 @@ module CtoD
23
24
  db
24
25
  end
25
26
 
27
+ desc "table_columns CSV", "Show column name and type pairs for a table based on given CSV"
28
+ option :string_size, aliases:"-s", default:100
29
+ def table_columns(csv)
30
+ csv_data = CSV.table(csv, header_converters:->h{h.strip})
31
+ columns = DB.build_columns(csv_data, string_size: options[:string_size])
32
+ puts "\e[32mcolumn name\e[0m :type"
33
+ puts "----------------------------"
34
+ puts columns.map { |name_type| "\e[32m%s\e[0m :%s" % name_type }
35
+ end
36
+
26
37
  desc "version", "Show CtoD version"
27
38
  def version
28
39
  puts "CtoD #{CtoD::VERSION} (c) 2013 kyoendo"
@@ -13,6 +13,7 @@ module CtoD
13
13
  @class_name = singularize(@table_name).capitalize
14
14
  @csv = CSV.table(csv, header_converters:->h{h.strip})
15
15
  @string_size = string_size
16
+ @table_columns = nil
16
17
  @uri = DB.connect(uri)
17
18
  end
18
19
 
@@ -25,13 +26,17 @@ module CtoD
25
26
  def create_table
26
27
  conn = AR.connection
27
28
  conn.create_table(@table_name) do |t|
28
- @csv.headers.zip(column_types).each do |name, type|
29
+ table_columns.each do |name, type|
29
30
  t.column name, type
30
31
  end
31
32
  t.timestamps
32
33
  end
33
34
  end
34
35
 
36
+ def table_columns
37
+ @table_columns ||= self.class.build_columns(@csv, string_size:@string_size)
38
+ end
39
+
35
40
  def export
36
41
  self.class.const_set(@class_name, Class.new(AR))
37
42
  self.class.const_get(@class_name).create! @csv.map(&:to_hash)
@@ -56,26 +61,26 @@ module CtoD
56
61
  puts "Something go wrong at connect: #{e}"
57
62
  end
58
63
 
59
- private
60
- def column_types
64
+ def self.build_columns(csv, string_size:100)
61
65
  is_date = /^\s*\d{1,4}(\-|\/)\d{1,2}(\-|\/)\d{1,2}\s*$/
62
- @csv.first.to_hash.inject([]) do |mem, (k, v)|
63
- mem << begin
66
+ csv.first.to_hash.inject({}) do |mem, (k, v)|
67
+ mem[k.intern] = begin
64
68
  case v
65
69
  when 'true', 'false'
66
70
  :boolean
67
71
  when is_date
68
72
  :date
69
73
  when String, Symbol
70
- @csv[k].compact.max_by(&:size).size > string_size ? :text : :string
74
+ csv[k].compact.max_by(&:size).size > string_size ? :text : :string
71
75
  when Fixnum, Float
72
- @csv[k].any? { |e| e.is_a? Float } ? :float : :integer
76
+ csv[k].any? { |e| e.is_a? Float } ? :float : :integer
73
77
  when NilClass
74
78
  :string
75
79
  else
76
80
  v.class.name.downcase.intern
77
81
  end
78
82
  end
83
+ mem
79
84
  end
80
85
  end
81
86
  end
@@ -1,3 +1,3 @@
1
1
  module CtoD
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,9 @@
1
+ year,name,designer,predecessor,date,version
2
+ 1995,Ruby,Yukihiro Matsumoto,Smalltalk / Perl,1995-1-1,2.1
3
+ 1959,LISP,John McCarthy,IPL,1959-02-01,3
4
+ 1972,Smalltalk,Daniel Henry Holmes Ingalls Jr. / Xerox PARC,Simula 67,1959/12/10,5.0
5
+ 1990,Haskell,Simon Peyton Jones,Miranda,1990-4-8,2
6
+ 1995,JavaScript,Brendan Eich,LiveScript,1995-11-11,
7
+ 1995,Java,James Gosling,C / Simula 67 / C++ / Smalltalk,,
8
+ 1993,Lua,Roberto Ierusalimschy,Scheme / SNOBOL / Modula / CLU,1993-1-1,5
9
+ 1987,Erlang,Joe Armstrong,Prolog,,
@@ -0,0 +1,102 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CtoD::DB do
6
+
7
+ class Lang < ActiveRecord::Base
8
+ end
9
+
10
+ def delete_db_if_exist
11
+ db_dir = '/tmp'
12
+ db = File.join(db_dir, 'test.db')
13
+ File.delete(db) if File.exist?(db)
14
+ end
15
+
16
+ def csvfile
17
+ csv_dir = File.join(File.dirname(__FILE__), '..', 'fixtures')
18
+ csv = File.join(csv_dir, 'langs.csv')
19
+ end
20
+
21
+ before do
22
+ delete_db_if_exist
23
+ end
24
+
25
+ after do
26
+ delete_db_if_exist
27
+ end
28
+
29
+ describe '#create_table and #table_exists?' do
30
+ context 'with csv filename and database uri' do
31
+ it "should be success and db is empty" do
32
+ conn = CtoD::DB.new(csv = csvfile, uri = 'sqlite3://localhost//tmp/test.db')
33
+ conn.table_exists?.should be_false
34
+ conn.create_table
35
+ langs = Lang.all
36
+ langs.length.should be 0
37
+ conn.table_exists?.should be_true
38
+ end
39
+ end
40
+ end
41
+
42
+ describe '.connect' do
43
+ context 'with database uri' do
44
+ it "should get URI::Generic class" do
45
+ conn = CtoD::DB.connect('sqlite3://localhost//tmp/test.db')
46
+ conn.class.should eql URI::Generic
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#export' do
52
+ context 'with csv filename and database uri' do
53
+ it "should be export csv file to database" do
54
+ conn = CtoD::DB.new(csv = csvfile, uri = 'sqlite3://localhost//tmp/test.db')
55
+ conn.table_exists?.should be_false
56
+ conn.create_table
57
+ conn.export
58
+
59
+ langs = Lang.all
60
+ langs.length.should be 8
61
+
62
+ lang = Lang.find(1)
63
+ lang.year.should eql 1995
64
+ lang.name.should eql "Ruby"
65
+ lang.designer.should eql "Yukihiro Matsumoto"
66
+ lang.predecessor.should eql "Smalltalk / Perl"
67
+
68
+ lang = Lang.find(8)
69
+ lang.year.should eql 1987
70
+ lang.name.should eql "Erlang"
71
+ lang.designer.should eql "Joe Armstrong"
72
+ lang.predecessor.should eql "Prolog"
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '#table_columns' do
78
+ it 'returns column name and type pairs in hash' do
79
+ conn = CtoD::DB.new(csv = csvfile, uri = 'sqlite3://localhost//tmp/test.db')
80
+ columns = conn.table_columns
81
+ columns[:year].should eql :integer
82
+ columns[:name].should eql :string
83
+ columns[:designer].should eql :string
84
+ columns[:predecessor].should eql :string
85
+ columns[:date].should eql :date
86
+ columns[:version].should eql :float
87
+ end
88
+ end
89
+
90
+ describe '.build_columns' do
91
+ it 'returns column name and type pairs in hash' do
92
+ csv = CSV.table(csvfile)
93
+ columns = CtoD::DB.build_columns(csv)
94
+ columns[:year].should eql :integer
95
+ columns[:name].should eql :string
96
+ columns[:designer].should eql :string
97
+ columns[:predecessor].should eql :string
98
+ columns[:date].should eql :date
99
+ columns[:version].should eql :float
100
+ end
101
+ end
102
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ctoD
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - kyoendo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-13 00:00:00.000000000 Z
11
+ date: 2013-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -144,6 +144,8 @@ files:
144
144
  - lib/ctoD/db.rb
145
145
  - lib/ctoD/version.rb
146
146
  - spec/ctoD_spec.rb
147
+ - spec/fixtures/langs.csv
148
+ - spec/lib/db_spec.rb
147
149
  - spec/spec_helper.rb
148
150
  homepage: https://github.com/melborne/ctoD
149
151
  licenses:
@@ -171,5 +173,7 @@ specification_version: 4
171
173
  summary: Export CSV data to database
172
174
  test_files:
173
175
  - spec/ctoD_spec.rb
176
+ - spec/fixtures/langs.csv
177
+ - spec/lib/db_spec.rb
174
178
  - spec/spec_helper.rb
175
179
  has_rdoc: