kandata 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b979ce1bd69f4da1bfa67689cd8e67f1b4e94c2e
4
+ data.tar.gz: c576026f129471fa86ae2d13989c942521642200
5
+ SHA512:
6
+ metadata.gz: a2cf47b93f63b3ffa4458e4cafe68dc17d0241319eb4a02b6bb58acb1243af234e2eae3ea9c5c5f678cd500105e4d0944febba4c1764b4a96a0cad94ecd9110e
7
+ data.tar.gz: 7f166fe8acf20a63bcd46cfaa7e9bdef54ec33adf23396a44076827a689e3f3bc3e4292c5c1fae214f1afa7c2f34c9eca904162502f1e78dab7847ace56c2864
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ /Gemfile.lock
14
+ /vendor/
15
+ /.idea
data/.reek ADDED
@@ -0,0 +1,14 @@
1
+ IrresponsibleModule:
2
+ enabled: false
3
+
4
+ TooManyStatements:
5
+ enabled: true
6
+ exclude:
7
+ - initialize
8
+ max_statements: 10
9
+
10
+ NestedIterators:
11
+ max_allowed_nesting: 3
12
+
13
+ DuplicateMethodCall:
14
+ max_calls: 2
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,28 @@
1
+ # Modify the version if you don't use MRI 2.1.
2
+ AllCops:
3
+ TargetRubyVersion: 2.4.3
4
+ Exclude:
5
+ - 'vendor/**/*'
6
+ - 'bin/**/*'
7
+
8
+ Rails:
9
+ # If you use RuboCop with Ruby on Rails, turn on this option.
10
+ Enabled: false
11
+
12
+ # You can customize rubocop settings.
13
+ # For example.
14
+ Bundler/OrderedGems:
15
+ Exclude:
16
+ - 'Gemfile'
17
+
18
+ Metrics/LineLength:
19
+ Max: 130
20
+
21
+ Style/Documentation:
22
+ Enabled: false
23
+
24
+ Style/AsciiComments:
25
+ Enabled: false
26
+
27
+ Layout/EmptyLineAfterMagicComment:
28
+ Enabled: false
@@ -0,0 +1 @@
1
+ 2.4.3
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+
4
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
5
+
6
+ # Specify your gem's dependencies in kandata.gemspec
7
+ gemspec
8
+
9
+ group :development do
10
+ gem 'guard'
11
+ gem 'guard-rspec'
12
+ gem 'guard-rubocop'
13
+ gem 'guard-reek'
14
+ gem 'terminal-notifier-guard'
15
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+ # A sample Guardfile
3
+ # More info at https://github.com/guard/guard#readme
4
+
5
+ ## Uncomment and set this to only include directories you want to watch
6
+ # directories %w(app lib config test spec features) \
7
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
8
+
9
+ ## Note: if you are using the `directories` clause above and you are not
10
+ ## watching the project directory ('.'), then you will want to move
11
+ ## the Guardfile to a watched dir and symlink it back, e.g.
12
+ #
13
+ # $ mkdir config
14
+ # $ mv Guardfile config/
15
+ # $ ln -s config/Guardfile .
16
+ #
17
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
18
+
19
+ # Note: The cmd option is now required due to the increasing number of ways
20
+ # rspec may be run, below are examples of the most common uses.
21
+ # * bundler: 'bundle exec rspec'
22
+ # * bundler binstubs: 'bin/rspec'
23
+ # * spring: 'bin/rspec' (This will use spring if running and you have
24
+ # installed the spring binstubs per the docs)
25
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
26
+ # * 'just' rspec: 'rspec'
27
+
28
+ group :red_green_refactor, halt_on_fail: true do
29
+ guard :rspec, cmd: 'bundle exec rspec' do
30
+ require 'guard/rspec/dsl'
31
+ dsl = Guard::RSpec::Dsl.new(self)
32
+
33
+ # Feel free to open issues for suggestions and improvements
34
+
35
+ # RSpec files
36
+ rspec = dsl.rspec
37
+ watch(rspec.spec_helper) { rspec.spec_dir }
38
+ watch(rspec.spec_support) { rspec.spec_dir }
39
+ watch(rspec.spec_files)
40
+
41
+ # Ruby files
42
+ ruby = dsl.ruby
43
+ dsl.watch_spec_files_for(ruby.lib_files)
44
+ end
45
+
46
+ guard :reek, all: 'exe lib' do
47
+ watch(%r{^(exe|lib)/.*\.rb$})
48
+ end
49
+
50
+ guard :rubocop do
51
+ watch(/.+\.rb$/)
52
+ watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ # Kandata
2
+
3
+ [![CircleCI](https://circleci.com/gh/tamano/kandata.svg?style=svg)](https://circleci.com/gh/tamano/kandata)
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kandata`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ TODO: Delete this and the text above, and describe your gem
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'kandata'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install kandata
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Development
30
+
31
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
32
+
33
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kandata.
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kandata"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ machine:
2
+ timezone:
3
+ Asia/Tokyo
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ #!/usr/bin/env ruby
3
+ require 'kandata.rb'
4
+
5
+ Kandata::CLI.start
@@ -0,0 +1,32 @@
1
+
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'kandata/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'kandata'
9
+ spec.version = Kandata::VERSION
10
+ spec.authors = ['Yuya TAMANO']
11
+ spec.email = ['everfree.main@gmail.com']
12
+
13
+ spec.summary = 'Provides function to import TSV file to MySQL table without create table DDL'
14
+ spec.homepage = 'https://github.com/tamano/kandata'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.16'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'reek'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'rubocop', '>= 0.52'
28
+ spec.add_development_dependency 'simplecov'
29
+
30
+ spec.add_dependency 'mysql2'
31
+ spec.add_dependency 'thor'
32
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ require 'kandata/database'
3
+ require 'kandata/tsv_file'
4
+ require 'kandata/version'
5
+ require 'kandata/cli'
6
+
7
+ class Kandata
8
+ # load_tsvの後に、ActiveRecordのインスタンスを返す
9
+ def self.build_from_tsv(filename, force_load)
10
+ _table_name = load_tsv(filename, force_load)
11
+
12
+ # TODO: ActiveRecordインスタンスの作成
13
+ end
14
+
15
+ # テーブルを作成し、テーブルにデータを読み込み、テーブル名を戻す
16
+ def self.load_tsv(filename, force_load)
17
+ client = Kandata::Database.new
18
+ client.load_tsv(filename, force_load)
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ require 'thor'
3
+
4
+ class Kandata
5
+ # command facade
6
+ class CLI < Thor
7
+ desc 'version', 'バージョン情報を表示する'
8
+ def version
9
+ puts Kandata::VERSION
10
+ end
11
+
12
+ desc 'load_tsv [FILENAME]', 'TSVファイルからテーブルを作成する'
13
+ method_option :force, type: :boolean, aliases: '-f', default: false, desc: '既にテーブルがある場合、一度削除して作成し直す'
14
+ def load_tsv(filename)
15
+ puts Kandata.load_tsv(filename, options['force'])
16
+ rescue StandardError => error
17
+ puts "ERROR: #{error.message}"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+ require 'mysql2'
3
+
4
+ class Kandata
5
+ # ActiveRecordを使わないでKandataデータベースにアクセスするためのクラス
6
+ class Database < Mysql2::Client
7
+ def initialize
8
+ create_database_if_not_exists
9
+ super(host: 'localhost', username: 'root', database: database_name, local_infile: true)
10
+ end
11
+
12
+ # ignore :reek:ControlParameter:
13
+ def load_tsv(filename, force_update)
14
+ tsv = Kandata::TsvFile.new(filename)
15
+
16
+ drop_table(tsv) if force_update
17
+ create_table(tsv)
18
+
19
+ load_data(tsv)
20
+ end
21
+
22
+ private
23
+
24
+ # ignore :reek:FeatureEnvy:
25
+ def create_database_if_not_exists
26
+ client = Mysql2::Client.new(host: 'localhost', username: 'root')
27
+
28
+ begin
29
+ client.query("SHOW CREATE DATABASE #{database_name}")
30
+ rescue Mysql2::Error => error
31
+ raise error unless error.message == "Unknown database '#{database_name}'"
32
+ client.query("CREATE DATABASE #{database_name}")
33
+ end
34
+ end
35
+
36
+ def drop_table(tsv)
37
+ query("DROP TABLE IF EXISTS #{full_table_name(tsv)}")
38
+ end
39
+
40
+ # ignore :reek:FeatureEnvy:
41
+ def create_table(tsv)
42
+ columns = tsv.headers
43
+
44
+ # 後で追加するため一旦削除
45
+ columns.delete('id')
46
+
47
+ columns_text = columns.map { |column| "#{column} VARCHAR(128)" }.join(',')
48
+ columns_text = 'id INTEGER AUTO_INCREMENT NOT NULL PRIMARY KEY,' + columns_text
49
+
50
+ query("CREATE TABLE #{full_table_name(tsv)} (#{columns_text})")
51
+ end
52
+
53
+ # ignore :reek:FeatureEnvy:
54
+ def load_data(tsv)
55
+ sql = "LOAD DATA LOCAL INFILE '#{tsv.filename}' INTO TABLE #{full_table_name(tsv)} IGNORE 1 LINES "
56
+ sql += "(#{tsv.headers.join(',')})"
57
+
58
+ query(sql)
59
+ full_table_name(tsv)
60
+ end
61
+
62
+ def database_name
63
+ 'kandata'
64
+ end
65
+
66
+ def full_table_name(tsv)
67
+ "#{database_name}.#{tsv.table_name}"
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ class Kandata
3
+ # 読み込み用TSVファイル
4
+ class TsvFile
5
+ attr_reader :filename, :headers
6
+
7
+ def initialize(filename)
8
+ @filename = filename
9
+ raise 'テーブル名に使用できないファイル名です' unless table_name.match?(/^[0-9a-zA-Z$_]+$/)
10
+
11
+ @headers = File.open(@filename, 'r').first(&:readline).chomp.split("\t")
12
+ validate_headers
13
+ end
14
+
15
+ def validate_headers
16
+ duplicated_columns = @headers.group_by { |value| value }.reject { |_key, value| value.one? }.keys
17
+ raise "以下のカラムが複数存在しています #{duplicated_columns}" unless duplicated_columns.empty?
18
+
19
+ illegal_named_columns = @headers.reject { |value| value.match?(/^[0-9a-zA-Z$_]+$/) }
20
+ raise "以下のカラム名は使用できません #{illegal_named_columns}" unless illegal_named_columns.empty?
21
+
22
+ # 現時点では、ミス防止のため、先頭以外にidカラムがあるとエラーとして処理する
23
+ raise '先頭以外にidカラムがあります' if include_id_column? && @headers[0] != 'id'
24
+ end
25
+
26
+ def table_name
27
+ File.basename(@filename).sub('.tsv', '')
28
+ end
29
+
30
+ def include_id_column?
31
+ @headers.include?('id')
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ class Kandata
3
+ VERSION = '0.1.0'
4
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kandata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuya TAMANO
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: reek
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0.52'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0.52'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mysql2
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: thor
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - everfree.main@gmail.com
128
+ executables:
129
+ - kandata
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".reek"
135
+ - ".rspec"
136
+ - ".rubocop.yml"
137
+ - ".ruby-version"
138
+ - Gemfile
139
+ - Guardfile
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - circle.yml
145
+ - exe/kandata
146
+ - kandata.gemspec
147
+ - lib/kandata.rb
148
+ - lib/kandata/cli.rb
149
+ - lib/kandata/database.rb
150
+ - lib/kandata/tsv_file.rb
151
+ - lib/kandata/version.rb
152
+ homepage: https://github.com/tamano/kandata
153
+ licenses: []
154
+ metadata: {}
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 2.6.14
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Provides function to import TSV file to MySQL table without create table
175
+ DDL
176
+ test_files: []