tanemaki 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0135427e518c02eaa6067e7b5799a85bee8cb7fd
4
- data.tar.gz: f010e5f25d1fe5379fe0c596312037080be72060
3
+ metadata.gz: bdc41e1779ca067b789c12ce2fada5b86719d094
4
+ data.tar.gz: fda55d2f676a52ac8d65e86dafe12b9ead144dcf
5
5
  SHA512:
6
- metadata.gz: 1345557d3e6261c4e7381e857c24273fe2707c215fc32928831bed9acbfef82288e37f54899b5a793ab4c509412f39bdd756c94d128fde7d3aa62fb01c12536d
7
- data.tar.gz: 486312d320566b28d07331973ef41aedcd5ac1c20d4dbc5b68612091e16fe0c2a041acd7ff7f593fdd020e9c2c1460c27175f121c1637134474b92054dbd3fc1
6
+ metadata.gz: 3fd272cd160de9f426ad42b5f40407b212cd4ef2f9b6c5bf1b4cebf73c852ca7a1fe4ec2f18f1b3e8e671411f20d11fd2293f7cad6ac5cb6bb207345f96370e9
7
+ data.tar.gz: 44f8af01d55abb92283eef0d2c89a821f0b85125a5745a465419a985e1502417e5394626dc1cbd66d1989e10acf4ecc1eff51f8fd3fffb078d75aa2868d58892
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  Tanemaki(たねまき)はRuby on Railsの`rake db:seed`で使われるseed.rbを整理するために書かれました。
6
6
 
7
7
  こうなっているのが
8
- ```
8
+ ```ruby
9
9
  AreaCode.create([
10
10
  {ken_code: 27, sityouson_code: 0, tiiki_code: 27000, ken_name: ,'大阪府', sityouson_name1: , sityouson_name2: '', sityouson_name3: , yomigana: 'おおさかふ'},
11
11
  {ken_code: 27, sityouson_code: 100, tiiki_code: 27100, ken_name: ,'大阪府', sityouson_name1: ,'大阪市', sityouson_name2: '', sityouson_name3: , yomigana: 'おおさかし'},
@@ -17,7 +17,7 @@ AreaCode.create([
17
17
  ```
18
18
 
19
19
  用意したCSVファイルと
20
- ```
20
+ ```csv
21
21
  ken_code,sityouson_code,tiiki_code,ken_name,sityouson_name1,sityouson_name2,sityouson_name3,yomigana
22
22
  27,0,27000,大阪府,,,,おおさかふ
23
23
  27,100,27100,大阪府,大阪市,,,おおさかし
@@ -27,40 +27,80 @@ ken_code,sityouson_code,tiiki_code,ken_name,sityouson_name1,sityouson_name2,sity
27
27
  27,128,27128,大阪府,大阪市,,中央区,ちゅうおうく
28
28
  ```
29
29
 
30
- ```
30
+ ```ruby
31
31
  AreaCode.tanemaki(csv_path).seed
32
32
  ```
33
33
 
34
34
  になります。
35
35
  ## Installation
36
-
37
- Add this line to your application's Gemfile:
38
-
39
36
  ```ruby
40
- gem 'tanemaki'
37
+ gem 'tanemaki', require: false
41
38
  ```
39
+ $ bundle install
40
+ 使う場所で
42
41
 
43
- And then execute:
44
-
45
- $ bundle
46
-
47
- Or install it yourself as:
48
-
49
- $ gem install tanemaki
42
+ require 'tanemaki'
50
43
 
51
44
  ## Usage
52
45
 
53
- TODO: Write usage instructions here
54
-
55
- ## Development
46
+ こういうmodelがあるとして
47
+ ```ruby
48
+ pry(main)> AreaCode
49
+ class AreaCode < ActiveRecord::Base {
50
+ :id => :integer,
51
+ :ken_code => :integer,
52
+ :sityouson_code => :integer,
53
+ :tiiki_code => :integer,
54
+ :ken_name => :string,
55
+ :sityouson_name1 => :string,
56
+ :sityouson_name2 => :string,
57
+ :sityouson_name3 => :string,
58
+ :yomigana => :string,
59
+ :created_at => :datetime,
60
+ :updated_at => :datetime
61
+ }
62
+ ```
63
+ `create`に必要なキーワード引数の**キーを一行目に書いたCSV**(例えば area_code.csv)を用意。
64
+ ```csv
65
+ ken_code,sityouson_code,tiiki_code,ken_name,sityouson_name1,sityouson_name2,sityouson_name3,yomigana
66
+ 27,0,27000,大阪府,,,,おおさかふ
67
+ 27,100,27100,大阪府,大阪市,,,おおさかし
68
+ 27,102,27102,大阪府,大阪市,,都島区,みやこじまく
69
+ 27,103,27103,大阪府,大阪市,,福島区,ふくしまく
70
+ # 略
71
+ 27,128,27128,大阪府,大阪市,,中央区,ちゅうおうく
72
+ ```
73
+ ゴー。
74
+ ```ruby
75
+ AreaCode.tanemaki('area_code.csv').seed
76
+ ```
77
+ 内部的には各行においてこのようなことが起こります。
78
+ ```ruby
79
+ AreaCode.create({ken_code: 27, sityouson_code: 102, tiiki_code: 27102, ken_name: ,'大阪府', sityouson_name1: ,'大阪市', sityouson_name2: '', sityouson_name3: '都島区', yomigana: 'みやこじまく'})
80
+ ```
81
+ 返り値は`create`の返り値の`Array`。
56
82
 
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.
83
+ ## create以外を使う
84
+ ぼくはどちらかというと`create!`が好きなので
85
+ ```ruby
86
+ AreaCode.tanemaki('area_code.csv', method: :create!).seed
87
+ ```
58
88
 
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).
89
+ ## select
60
90
 
61
- ## Contributing
91
+ CSVから必要なカラムのみ使います。
92
+ ```ruby
93
+ AreaCode.tanemaki('area_code.csv').select(:tiiki_code, :ken_name, :sityouson_name1, :sityouson_name3).seed
94
+ ```
95
+ 内部的には各行においてこのようなことが起こります。
96
+ ```ruby
97
+ AreaCode.create({tiiki_code: 27102, ken_name: ,'大阪府', sityouson_name1: ,'大阪市', sityouson_name3: '都島区'})
98
+ ```
62
99
 
63
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tanemaki.
100
+ ## evaluate
101
+ 指定したカラムの値を式として評価します。
102
+ インスタンス変数の参照先は`scope: self`などと設定します。`Tanemaki.default_eva_scope =self`なども可能です。
103
+ (記述中)
64
104
 
65
105
 
66
106
  ## License
data/lib/tanemaki.rb CHANGED
@@ -14,6 +14,11 @@ module Tanemaki
14
14
 
15
15
 
16
16
  def default_eval_scope(eval_scope)
17
+ self.default_eval_scope = eval_scope
18
+ end
19
+
20
+
21
+ def default_eval_scope=(eval_scope)
17
22
  @eval_scope = eval_scope
18
23
  end
19
24
  end
@@ -28,11 +33,25 @@ module Tanemaki
28
33
 
29
34
  def ready(path)
30
35
  header, *lines = CSV.read(path)
31
- header.map!(&:to_sym)
36
+ nameless = []
37
+ readiness = header.map.with_index do |name, index|
38
+ if name
39
+ name.to_sym
40
+ else
41
+ nameless.push(index)
42
+ nil
43
+ end
44
+ end
45
+
32
46
  lines.map do |line|
47
+ nameless_parameter = []
33
48
  line.each_with_index.each_with_object({}) do |(col, index), result|
34
- result[header[index]] = col if col
35
- end
49
+ if nameless.include?(index)
50
+ nameless_parameter.push(col) if col
51
+ else
52
+ result[readiness[index]] = col if col
53
+ end
54
+ end.merge(namelass_parameter_array: nameless_parameter)
36
55
  end
37
56
  end
38
57
  end
@@ -71,13 +90,17 @@ module Tanemaki
71
90
 
72
91
  def seed(klass = nil, method = nil, &block)
73
92
  @named_csv.map do |row|
74
- readiness = evaluated(row)
93
+ readiness, nameless = begin
94
+ result = evaluated(row.dup)
95
+ [result, result.delete(:namelass_parameter_array)]
96
+ end
97
+
75
98
  begin
76
- (klass || @klass).send((method || @method), **readiness)
99
+ (klass || @klass).send((method || @method), *nameless, **readiness)
77
100
  rescue => e
78
101
  raise e unless block_given?
79
102
 
80
- block.(row, e)
103
+ block.(e, row)
81
104
  nil
82
105
  end
83
106
  end.compact
@@ -97,19 +120,28 @@ module Tanemaki
97
120
  def evaluated(row)
98
121
  return row if @evaluate.size == 0
99
122
 
100
- row.each_pair.each_with_object({}) do |(k, v), result|
101
- next result[k] = v unless @evaluate.include?(k)
123
+ row[:namelass_parameter_array].map!.with_index do |value, index|
124
+ next value unless @evaluate.include?(index)
102
125
 
103
- result[k] = begin
104
- return eval(v) unless @eval_scope
126
+ do_eval(value)
127
+ end
105
128
 
106
- @eval_scope.instance_eval do
107
- eval(v)
108
- end
109
- rescue
110
- v
111
- end
129
+ row.each_pair.each_with_object({}) do |(key, value), result|
130
+ next result[key] = value unless @evaluate.include?(key)
131
+
132
+ result[key] = do_eval(value)
133
+ end
134
+ end
135
+
136
+
137
+ def do_eval(value)
138
+ return eval(value) unless @eval_scope
139
+
140
+ @eval_scope.instance_eval do
141
+ eval(value)
112
142
  end
143
+ rescue
144
+ value
113
145
  end
114
146
 
115
147
 
@@ -1,3 +1,3 @@
1
1
  module Tanemaki
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
data/tanemaki.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.description = 'Seeding with CSV having named column.'
14
14
  spec.homepage = 'http://mmmpa.net/'
15
15
  spec.license = 'MIT'
16
-
16
+
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
18
  spec.bindir = "exe"
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tanemaki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - mmmpa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-20 00:00:00.000000000 Z
11
+ date: 2015-06-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler