tanemaki 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0135427e518c02eaa6067e7b5799a85bee8cb7fd
4
+ data.tar.gz: f010e5f25d1fe5379fe0c596312037080be72060
5
+ SHA512:
6
+ metadata.gz: 1345557d3e6261c4e7381e857c24273fe2707c215fc32928831bed9acbfef82288e37f54899b5a793ab4c509412f39bdd756c94d128fde7d3aa62fb01c12536d
7
+ data.tar.gz: 486312d320566b28d07331973ef41aedcd5ac1c20d4dbc5b68612091e16fe0c2a041acd7ff7f593fdd020e9c2c1460c27175f121c1637134474b92054dbd3fc1
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.5
5
+ - 2.2.2
6
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tanemaki.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 mmmpa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ [![Build Status](https://travis-ci.org/mmmpa/tanemaki.svg)](https://travis-ci.org/mmmpa/tanemaki)
2
+
3
+ # Tanemaki
4
+
5
+ Tanemaki(たねまき)はRuby on Railsの`rake db:seed`で使われるseed.rbを整理するために書かれました。
6
+
7
+ こうなっているのが
8
+ ```
9
+ AreaCode.create([
10
+ {ken_code: 27, sityouson_code: 0, tiiki_code: 27000, ken_name: ,'大阪府', sityouson_name1: , sityouson_name2: '', sityouson_name3: , yomigana: 'おおさかふ'},
11
+ {ken_code: 27, sityouson_code: 100, tiiki_code: 27100, ken_name: ,'大阪府', sityouson_name1: ,'大阪市', sityouson_name2: '', sityouson_name3: , yomigana: 'おおさかし'},
12
+ {ken_code: 27, sityouson_code: 102, tiiki_code: 27102, ken_name: ,'大阪府', sityouson_name1: ,'大阪市', sityouson_name2: '', sityouson_name3: '都島区', yomigana: 'みやこじまく'},
13
+ {ken_code: 27, sityouson_code: 103, tiiki_code: 27103, ken_name: ,'大阪府', sityouson_name1: ,'大阪市', sityouson_name2: '', sityouson_name3: '福島区', yomigana: 'ふくしまく'},
14
+ # 略
15
+ {ken_code: 27, sityouson_code: 128, tiiki_code: 27128, ken_name: ,'大阪府', sityouson_name1: ,'大阪市', sityouson_name2: '', sityouson_name3: '中央区', yomigana: 'ちゅうおうく'},
16
+ ])
17
+ ```
18
+
19
+ 用意したCSVファイルと
20
+ ```
21
+ ken_code,sityouson_code,tiiki_code,ken_name,sityouson_name1,sityouson_name2,sityouson_name3,yomigana
22
+ 27,0,27000,大阪府,,,,おおさかふ
23
+ 27,100,27100,大阪府,大阪市,,,おおさかし
24
+ 27,102,27102,大阪府,大阪市,,都島区,みやこじまく
25
+ 27,103,27103,大阪府,大阪市,,福島区,ふくしまく
26
+ # 略
27
+ 27,128,27128,大阪府,大阪市,,中央区,ちゅうおうく
28
+ ```
29
+
30
+ ```
31
+ AreaCode.tanemaki(csv_path).seed
32
+ ```
33
+
34
+ になります。
35
+ ## Installation
36
+
37
+ Add this line to your application's Gemfile:
38
+
39
+ ```ruby
40
+ gem 'tanemaki'
41
+ ```
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ Or install it yourself as:
48
+
49
+ $ gem install tanemaki
50
+
51
+ ## Usage
52
+
53
+ TODO: Write usage instructions here
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ 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).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tanemaki.
64
+
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
69
+
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ task :default => :spec
4
+ RSpec::Core::RakeTask.new
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tanemaki"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Tanemaki
2
+ VERSION = "0.1.0"
3
+ end
data/lib/tanemaki.rb ADDED
@@ -0,0 +1,135 @@
1
+ require 'tanemaki/version'
2
+ require 'csv'
3
+
4
+ module Tanemaki
5
+ class << self
6
+ def call(*args)
7
+ ready(*args)
8
+ end
9
+
10
+
11
+ def ready(path, options = {})
12
+ Seeder.(Parser.(path), {eval_scope: @eval_scope}.merge(options))
13
+ end
14
+
15
+
16
+ def default_eval_scope(eval_scope)
17
+ @eval_scope = eval_scope
18
+ end
19
+ end
20
+
21
+
22
+ class Parser
23
+ class << self
24
+ def call(*args)
25
+ ready(*args)
26
+ end
27
+
28
+
29
+ def ready(path)
30
+ header, *lines = CSV.read(path)
31
+ header.map!(&:to_sym)
32
+ lines.map do |line|
33
+ line.each_with_index.each_with_object({}) do |(col, index), result|
34
+ result[header[index]] = col if col
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+
42
+ class Seeder
43
+ def self.call(*args)
44
+ new(*args)
45
+ end
46
+
47
+
48
+ def initialize(named_csv, options = {})
49
+ @named_csv = named_csv
50
+ @evaluate = options[:evaluatable] || []
51
+ @eval_scope = options[:eval_scope]
52
+ @klass = options[:klass]
53
+ @method = options[:method] || :create
54
+ end
55
+
56
+
57
+ def evaluate(*column_names, eval_scope: nil)
58
+ Seeder.(@named_csv, for_chain.merge(eval_scope: eval_scope || @eval_scope, evaluatable: column_names))
59
+ end
60
+
61
+
62
+ def named_csv
63
+ @named_csv
64
+ end
65
+
66
+
67
+ def random(klass = nil, method_name = nil, &block)
68
+ Seeder.(@named_csv.shuffle, for_chain).seed(klass, method_name, &block)
69
+ end
70
+
71
+
72
+ def seed(klass = nil, method = nil, &block)
73
+ @named_csv.map do |row|
74
+ readiness = evaluated(row)
75
+ begin
76
+ (klass || @klass).send((method || @method), **readiness)
77
+ rescue => e
78
+ raise e unless block_given?
79
+
80
+ block.(row, e)
81
+ nil
82
+ end
83
+ end.compact
84
+ end
85
+
86
+
87
+ def select(*column_names)
88
+ Seeder.(@named_csv.map do |row|
89
+ column_names.each_with_object({}) do |name, new_row|
90
+ new_row[name] = row[name]
91
+ end
92
+ end, for_chain)
93
+ end
94
+
95
+
96
+ private
97
+ def evaluated(row)
98
+ return row if @evaluate.size == 0
99
+
100
+ row.each_pair.each_with_object({}) do |(k, v), result|
101
+ next result[k] = v unless @evaluate.include?(k)
102
+
103
+ result[k] = begin
104
+ return eval(v) unless @eval_scope
105
+
106
+ @eval_scope.instance_eval do
107
+ eval(v)
108
+ end
109
+ rescue
110
+ v
111
+ end
112
+ end
113
+ end
114
+
115
+
116
+ def for_chain
117
+ {
118
+ evaluatable: @evaluate,
119
+ eval_scope: @eval_scope,
120
+ klass: @klass,
121
+ method: @method
122
+ }
123
+ end
124
+ end
125
+ end
126
+
127
+
128
+ class Object
129
+ def tanemaki(path, options = {})
130
+ Tanemaki.(path, options.merge(klass: self))
131
+ end
132
+
133
+
134
+ alias :tnmk :tanemaki
135
+ end
data/tanemaki.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tanemaki/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tanemaki"
8
+ spec.version = Tanemaki::VERSION
9
+ spec.authors = ['mmmpa']
10
+ spec.email = ['mmmpa.mmmpa@gmail.com']
11
+
12
+ spec.summary = 'Seeding with CSV having named column.'
13
+ spec.description = 'Seeding with CSV having named column.'
14
+ spec.homepage = 'http://mmmpa.net/'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tanemaki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mmmpa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-20 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: rspec
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
+ description: Seeding with CSV having named column.
56
+ email:
57
+ - mmmpa.mmmpa@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/tanemaki.rb
71
+ - lib/tanemaki/version.rb
72
+ - tanemaki.gemspec
73
+ homepage: http://mmmpa.net/
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Seeding with CSV having named column.
97
+ test_files: []