mi 0.1.0 → 0.2.0

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: 4d74b095ca988d1bb68b286520b448aadb8100ff
4
- data.tar.gz: 73be5672f5afea9b1f61e60669bc5aca7f49f739
3
+ metadata.gz: c5533081b911650aae4e4bf66883993171d4e7d1
4
+ data.tar.gz: 73392df3edede1d9cdf83352cddc404fc5c5cad0
5
5
  SHA512:
6
- metadata.gz: 731fee1baa7d74572d22d5bd0703104d6699b27cdeb8ebc5fcad4b5c11184b59bf70c728dff926bfa8f3339d1c0c3e8d96d35fda197fd49793e29424a769553e
7
- data.tar.gz: 05f0c2b97aac8ec4e05b7ec63753b719be6bce2b9ce204641bdb59101ebd683221cd421173334948879c7a211d58185ce79428d209fefef5c7f451a2ae9d2fcb
6
+ metadata.gz: b3e48cdf0545ea2e6f3f6c6869068819c94a59604f0b568cae34b79441fb282810f0b18bd3bafff297c375df813ba607a73b8043a8e9febedfe55d0a7ed5ab61
7
+ data.tar.gz: ae694398b415e948f5accf23f7f7e4c7a4a8030db5f0019951611aca4f90cc14dc27b3016273bd5d23197b2022f4c2acae3c89aa772d20bb5fed0806194a6191
@@ -2,7 +2,7 @@ language: ruby
2
2
 
3
3
  rvm:
4
4
  - 2.2.4
5
- - 2.3.0
5
+ - 2.3.1
6
6
  - ruby-head
7
7
 
8
8
  cache: bundler
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/mi.svg)](https://badge.fury.io/rb/mi)
4
4
  [![Build Status](https://travis-ci.org/pocke/mi.svg?branch=master)](https://travis-ci.org/pocke/mi)
5
+ [![Coverage Status](https://coveralls.io/repos/github/pocke/mi/badge.svg?branch=master)](https://coveralls.io/github/pocke/mi?branch=master)
6
+ [![Stories in Ready](https://badge.waffle.io/pocke/mi.svg?label=ready&title=Ready)](http://waffle.io/pocke/mi)
5
7
 
6
8
  `mi` is a generator of migration file instead of `rails generate migration`.
7
9
 
@@ -13,7 +15,9 @@
13
15
  Add this line to your application's Gemfile:
14
16
 
15
17
  ```ruby
16
- gem 'mi'
18
+ group :development do
19
+ gem 'mi'
20
+ end
17
21
  ```
18
22
 
19
23
  And then execute:
@@ -86,7 +90,6 @@ end
86
90
 
87
91
  ## TODOs
88
92
 
89
- - Support `create_talbe` [#9](https://github.com/pocke/mi/issues/9)
90
93
  - Support Rails 5 [#12](https://github.com/pocke/mi/issues/12)
91
94
 
92
95
 
@@ -94,3 +97,7 @@ end
94
97
 
95
98
  Bug reports and pull requests are welcome on GitHub at https://github.com/pocke/mi.
96
99
 
100
+
101
+ ## Links
102
+
103
+ - [もっと便利に rails g migration する - pockestrap](http://pocke.hatenablog.com/entry/2016/05/01/132228)
@@ -0,0 +1,71 @@
1
+ require 'rails'
2
+ require 'rails/generators'
3
+ require 'active_record'
4
+ require 'strscan'
5
+
6
+
7
+ module Mi
8
+ module Generators
9
+ class Base < Rails::Generators::Base
10
+ include Rails::Generators::Migration
11
+
12
+ def self.next_migration_number(dirname)
13
+ next_migration_number = current_migration_number(dirname) + 1
14
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
15
+ end
16
+
17
+ Methods = {
18
+ '+' => 'add_column',
19
+ '-' => 'remove_column',
20
+ '%' => 'change_column',
21
+ }.freeze
22
+
23
+ def version
24
+ if arguments.include?('--version')
25
+ puts Mi::VERSION
26
+ exit 0 # XXX:
27
+ end
28
+ end
29
+
30
+
31
+ private
32
+
33
+ def arguments
34
+ @_initializer[0..1].flatten
35
+ end
36
+
37
+ def arg_groups
38
+ @arg_groups ||= (
39
+ args = arguments.reject{|x| x.start_with?('--')}
40
+
41
+ current = nil
42
+ res = args.group_by do |a|
43
+ if %w[+ - %].include? a[0]
44
+ current
45
+ else
46
+ current = a
47
+ nil
48
+ end
49
+ end
50
+ res.delete(nil)
51
+ res
52
+ )
53
+ end
54
+
55
+ # TODO: parse options
56
+ # @param [String] col +COL_NAME:TYPE:{OPTIONS}
57
+ def parse_column(col)
58
+ sc = StringScanner.new(col)
59
+ name = sc.scan(/[^:]+/)
60
+ sc.scan(/:/)
61
+ type = sc.scan(/[^:]+/)
62
+ sc.scan(/:/)
63
+ options = sc.scan(/\{.+\}$/)
64
+
65
+ method = name[0]
66
+ name = name[1..-1]
67
+ {name: name, type: type, options: options, method: method}
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,49 @@
1
+ require 'generators/mi'
2
+
3
+ module Mi
4
+ module Generators
5
+ class CreateGenerator < Base
6
+ class TypeIsRequired < StandardError; end
7
+ class NotAllowMethod < StandardError; end
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def doing
12
+ migration_template('create.rb.erb', "db/migrate/#{destination}.rb")
13
+ end
14
+
15
+
16
+ private
17
+
18
+
19
+ # returns t.TYPE :NAME, OPTIONS: true
20
+ # e.g.) t.string :email
21
+ def to_method(col)
22
+ info = parse_column(col)
23
+ # TODO: when type is not specified, migration file would be created.
24
+ raise TypeIsRequired, "When mi:create, type is required. Please specify type like `#{info[:name]}:TYPE`" unless info[:type]
25
+ raise NotAllowMethod, "#{info[:method]} is not allowed. You can only use `+` when `mi:create`" unless info[:method] == '+'
26
+
27
+ res = "t.#{info[:type]} :#{info[:name]}"
28
+
29
+ return res unless info[:options]
30
+ # TODO: DRY
31
+ res << ", #{info[:options][1..-2].gsub(':', ': ').gsub(',', ', ')}"
32
+
33
+ res
34
+ end
35
+
36
+ # when create table, table name is only one.
37
+ def table_name
38
+ @table_name ||= (
39
+ table, = *arg_groups.first
40
+ table.tableize
41
+ )
42
+ end
43
+
44
+ def destination
45
+ "create_#{table_name}_table"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,113 +1,60 @@
1
- require 'rails'
2
- require 'rails/generators'
3
- require 'active_record'
4
- require 'strscan'
1
+ require 'generators/mi'
5
2
 
3
+ module Mi
4
+ module Generators
5
+ class MiGenerator < Base
6
+ source_root File.expand_path('../templates', __FILE__)
7
+ namespace "mi"
6
8
 
7
- class MiGenerator < Rails::Generators::Base
8
- include Rails::Generators::Migration
9
-
10
- def self.next_migration_number(dirname)
11
- next_migration_number = current_migration_number(dirname) + 1
12
- ActiveRecord::Migration.next_migration_number(next_migration_number)
13
- end
14
-
15
- Methods = {
16
- '+' => 'add_column',
17
- '-' => 'remove_column',
18
- '%' => 'change_column',
19
- }.freeze
20
-
21
- source_root File.expand_path('../templates', __FILE__)
9
+ def doing
10
+ migration_template('migration.rb.erb', "db/migrate/#{destination}.rb")
11
+ end
22
12
 
23
- def doing
24
- if arguments.include?('--version')
25
- puts Mi::VERSION
26
- return
27
- end
28
13
 
29
- migration_template('migration.rb.erb', "db/migrate/#{destination}.rb")
30
- end
14
+ private
31
15
 
32
16
 
33
- private
17
+ # @param [String] col +COL_NAME:TYPE:{OPTIONS}
18
+ def to_method(table, col)
19
+ info = parse_column(col)
20
+ res = "#{Methods[info[:method]]} :#{table}, :#{info[:name]}"
34
21
 
35
- def arguments
36
- @_initializer[0..1].flatten
37
- end
22
+ return res unless info[:type]
23
+ res << ", :#{info[:type]}"
38
24
 
39
- def arg_groups
40
- @arg_groups ||= (
41
- args = arguments.reject{|x| x.start_with?('--')}
25
+ return res unless info[:options]
26
+ res << ", #{info[:options][1..-2].gsub(':', ': ').gsub(',', ', ')}"
42
27
 
43
- current = nil
44
- res = args.group_by do |a|
45
- if %w[+ - %].include? a[0]
46
- current
47
- else
48
- current = a
49
- nil
50
- end
28
+ res
51
29
  end
52
- res.delete(nil)
53
- res
54
- )
55
- end
56
-
57
- # TODO: parse options
58
- # @param [String] col +COL_NAME:TYPE:{OPTIONS}
59
- def parse_column(col)
60
- sc = StringScanner.new(col)
61
- name = sc.scan(/[^:]+/)
62
- sc.scan(/:/)
63
- type = sc.scan(/[^:]+/)
64
- sc.scan(/:/)
65
- options = sc.scan(/\{.+\}$/)
66
30
 
67
- method = name[0]
68
- name = name[1..-1]
69
- {name: name, type: type, options: options, method: method}
70
- end
71
-
72
- # @param [String] col +COL_NAME:TYPE:{OPTIONS}
73
- def to_method(table, col)
74
- info = parse_column(col)
75
- res = "#{Methods[info[:method]]} :#{table}, :#{info[:name]}"
76
-
77
- return res unless info[:type]
78
- res << ", :#{info[:type]}"
79
-
80
- return res unless info[:options]
81
- res << ", #{info[:options][1..-2].gsub(':', ': ').gsub(',', ', ')}"
82
-
83
- res
84
- end
85
-
86
- def to_dest(col)
87
- parsed = parse_column(col)
88
- verb =
89
- case parsed[:method]
90
- when '+'
91
- 'add'
92
- when '-'
93
- 'remove'
94
- when '%'
95
- 'change'
31
+ def to_dest(col)
32
+ parsed = parse_column(col)
33
+ verb =
34
+ case parsed[:method]
35
+ when '+'
36
+ 'add'
37
+ when '-'
38
+ 'remove'
39
+ when '%'
40
+ 'change'
41
+ end
42
+ [verb, parsed[:name]]
96
43
  end
97
- [verb, parsed[:name]]
98
- end
99
44
 
100
- def destination
101
- table, columns = *arg_groups.first
102
- c = {
103
- '+' => 'to',
104
- '-' => 'from',
105
- '%' => 'of'
106
- }[parse_column(columns.last)[:method]]
107
- [
108
- columns.map{|c| to_dest(c)}.inject{|sum, x| sum.concat(['and', x].flatten)},
109
- c,
110
- table.tableize,
111
- ].flatten.join('_')
45
+ def destination
46
+ table, columns = *arg_groups.first
47
+ c = {
48
+ '+' => 'to',
49
+ '-' => 'from',
50
+ '%' => 'of',
51
+ }[parse_column(columns.last)[:method]]
52
+ [
53
+ columns.map{|c| to_dest(c)}.inject{|sum, x| sum.concat(['and', x].flatten)},
54
+ c,
55
+ table.tableize,
56
+ ].flatten.join('_')
57
+ end
58
+ end
112
59
  end
113
60
  end
@@ -0,0 +1,13 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def change
3
+ create_table :<%= table_name %> do |t|
4
+ <%- arg_groups.each do |_table, columns| -%>
5
+ <%- columns.each do |column| -%>
6
+ <%= to_method(column) %>
7
+ <%- end -%>
8
+ <%- end -%>
9
+
10
+ t.timestamps null: false
11
+ end
12
+ end
13
+ end
data/lib/mi.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require "mi/version"
2
-
3
- module Mi
4
- # Your code goes here...
5
- end
2
+ require 'generators/mi'
3
+ require 'generators/mi/mi_generator'
4
+ require 'generators/mi/create_generator'
@@ -1,3 +1,3 @@
1
1
  module Mi
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
data/mi.gemspec CHANGED
@@ -18,13 +18,14 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
  spec.licenses = ['CC0-1.0']
21
+ spec.required_ruby_version = '>= 2.2.0'
21
22
 
22
23
  spec.add_runtime_dependency 'rails', '>= 4.0.0'
23
24
  spec.add_runtime_dependency 'activerecord', '>= 4.0.0'
24
25
 
25
26
  spec.add_development_dependency "bundler", "~> 1.11"
26
27
  spec.add_development_dependency "rake", "~> 10.0"
27
- spec.add_development_dependency 'rubocop', '~> 0.39.0'
28
+ spec.add_development_dependency 'rubocop', '~> 0.41.1'
28
29
 
29
30
  # testing
30
31
  spec.add_development_dependency "rspec", "~> 3.4.0"
@@ -33,4 +34,6 @@ Gem::Specification.new do |spec|
33
34
  spec.add_development_dependency 'guard-bundler', '~> 2.1.0'
34
35
  spec.add_development_dependency 'pry', '~> 0.10.3'
35
36
  spec.add_development_dependency 'rspec-power_assert', '~> 0.3.0'
37
+ spec.add_development_dependency 'coveralls', '~> 0.8.13'
38
+ spec.add_development_dependency 'simplecov', '~> 0.11.0'
36
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masataka Kuwabara
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-01 00:00:00.000000000 Z
11
+ date: 2016-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.39.0
75
+ version: 0.41.1
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.39.0
82
+ version: 0.41.1
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -164,6 +164,34 @@ dependencies:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
166
  version: 0.3.0
167
+ - !ruby/object:Gem::Dependency
168
+ name: coveralls
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 0.8.13
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 0.8.13
181
+ - !ruby/object:Gem::Dependency
182
+ name: simplecov
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: 0.11.0
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: 0.11.0
167
195
  description: Mi is a generator of migration file instead of `rails generate migration`.
168
196
  email:
169
197
  - p.ck.t22@gmail.com
@@ -182,7 +210,10 @@ files:
182
210
  - Rakefile
183
211
  - bin/console
184
212
  - bin/setup
213
+ - lib/generators/mi.rb
214
+ - lib/generators/mi/create_generator.rb
185
215
  - lib/generators/mi/mi_generator.rb
216
+ - lib/generators/mi/templates/create.rb.erb
186
217
  - lib/generators/mi/templates/migration.rb.erb
187
218
  - lib/mi.rb
188
219
  - lib/mi/version.rb
@@ -199,7 +230,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
199
230
  requirements:
200
231
  - - ">="
201
232
  - !ruby/object:Gem::Version
202
- version: '0'
233
+ version: 2.2.0
203
234
  required_rubygems_version: !ruby/object:Gem::Requirement
204
235
  requirements:
205
236
  - - ">="