ittan 0.1.3 → 0.1.4

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: 68e8beadd6fe1bfc4b7e766ed2fc764838db08b1
4
- data.tar.gz: 6d4610306ea854b7e89fd5e0ff643a5daa11b607
3
+ metadata.gz: c577645dacb78fc36de48a3af7292f0316671abf
4
+ data.tar.gz: a8c3d9e5b35e98c42f4429a885698f0c5beca49e
5
5
  SHA512:
6
- metadata.gz: 0d0ccc2008516fe8c95e2becc8ff0dd44a0bd2d1fc4afabc506f81ea83409b99ae4f1c7a9609b5924ce25fd9e1033ac4ab71dfbb8b3090d64675fae13ba299b4
7
- data.tar.gz: 745b1a7b8e7762f067639d2b9758e48bbdbfc3fd0999ce1b8699074d3087ddad365b60baae8005d40e9f32a0333f067b67b557c0dcd77084562fee63bc4ca287
6
+ metadata.gz: 3da812d8e54aca03d432d924f914ee32d22a3a9b5c47a35573dae565373f746dd251140cd4e620292a69749e5b8e26ecb7c6cce2045a47393501828cb2465ccb
7
+ data.tar.gz: 375d802584f396022296670645ce346a59bc5be7cb26c67cab144cde9074c3ae3d892449271d5bd1954a56c66442aef8645408e074e0b49adf9dee5ef1e76c38
data/Gemfile CHANGED
@@ -1,10 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'awesome_print'
4
- gem 'thor'
5
- gem 'faker'
6
- gem 'tod'
7
- gem 'activesupport'
8
-
9
- # Specify your gem's dependencies in ittan.gemspec
10
3
  gemspec
data/README.md CHANGED
@@ -9,21 +9,13 @@ Ittan (一旦) is a Gem that generates seed file from schema.rb for the time bei
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'ittan'
12
+ gem install ittan
13
13
  ```
14
14
 
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install ittan
22
-
23
15
  ## Usage
24
16
 
25
17
  ```
26
- $ bundle exec ittan
18
+ $ ittan
27
19
  ```
28
20
 
29
21
  ## Contributing
data/ittan.gemspec CHANGED
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rspec", "~> 3.0"
25
25
 
26
26
  spec.add_runtime_dependency "thor"
27
+ spec.add_runtime_dependency "faker"
27
28
  spec.add_runtime_dependency "tod"
28
29
  spec.add_runtime_dependency "activesupport"
29
30
  end
data/lib/ittan.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "ittan/version"
2
2
  require "ittan/cli"
3
3
  require "ittan/table"
4
+ require "ittan/column"
4
5
 
5
6
  module Ittan
6
7
  end
data/lib/ittan/cli.rb CHANGED
@@ -1,33 +1,25 @@
1
1
  require 'thor'
2
- require 'faker'
3
- require 'tod'
4
- require 'tod/core_extensions'
5
- require 'active_support'
6
- require 'active_support/core_ext'
7
- require 'awesome_print'
8
2
 
9
3
  module Ittan
10
-
11
4
  class CLI < Thor
12
5
  default_command :create
13
6
 
14
7
  desc "create", "create dummy data."
15
8
  # method_option :type, aliases: '-t', default: 'seed_fu', type: :string, desc: "Select seed type. (seed or seed_fu)"
16
- def create(file_path = 'db/schema.rb')
9
+ def create(schema_file_path = 'db/schema.rb')
17
10
  begin
18
- File.open(file_path) do |file|
19
- file.read.split("create_table").each_with_index do |table_sentence, index|
11
+ File.open(schema_file_path) do |schema_file|
12
+ tables = schema_file.read.split("create_table")
13
+ tables.each_with_index do |table, index|
20
14
  next if index == 0 # skip comment
21
15
 
22
- table_instance = Ittan::Table.new(table_sentence)
16
+ table_instance = Ittan::Table.new(table)
23
17
  table_instance.create_fixtures_directory
24
18
  table_instance.create_seed_file
25
19
  end
26
20
  end
27
- rescue SystemCallError => e
28
- puts %Q(class=[#{e.class}] message=[#{e.message}])
29
- rescue IOError => e
30
- puts %Q(class=[#{e.class}] message=[#{e.message}])
21
+ rescue SystemCallError, IOError => e
22
+ puts %Q(\e[31mError: #{e.message}\e[0m)
31
23
  end
32
24
  end
33
25
  end
@@ -0,0 +1,100 @@
1
+ require 'faker'
2
+ require 'tod'
3
+ require 'tod/core_extensions'
4
+
5
+ module Ittan
6
+ class Column
7
+ attr_accessor :text, :seed_file, :name
8
+
9
+ def initialize(column_text, seed_file)
10
+ @text = column_text
11
+ @seed_file = seed_file
12
+ @name = extract_column_name
13
+ end
14
+
15
+ def input_dummy_data
16
+ input_file create_string_dummy if @text.include?('t.string')
17
+ input_file create_text_dummy if @text.include?('t.text')
18
+ input_file create_integer_dummy if @text.include?('t.integer')
19
+ input_file create_float_dummy if @text.include?('t.float')
20
+ input_file create_decimal_dummy if @text.include?('t.decimal')
21
+ input_file create_boolean_dummy if @text.include?('t.boolean')
22
+ input_file create_date_dummy if @text.include?('t.date ')
23
+ input_file create_time_dummy if @text.include?('t.time')
24
+ input_file create_datetime_dummy if @text.include?('t.datetime')
25
+ end
26
+
27
+ private
28
+
29
+ def extract_column_name
30
+ @text.match("\"[a-zA-Z0-9_]*\"")[0].delete("\"")
31
+ end
32
+
33
+ def extract_column_limit
34
+ @text.match("(limit: )[0-9]*") { |match_data| match_data[0].delete("^0-9").to_i }
35
+ end
36
+
37
+ def extract_column_precision
38
+ @text.match("(precision: )[0-9]*") { |match_data| match_data[0].delete("^0-9").to_i }
39
+ end
40
+
41
+ def extract_column_scale
42
+ @text.match("(scale: )[0-9]*") { |match_data| match_data[0].delete("^0-9").to_i }
43
+ end
44
+
45
+ def create_string_dummy
46
+ limit = extract_column_limit
47
+ limit_random = rand(1..limit)
48
+ "\"#{Faker::Lorem.characters(limit_random)}\""
49
+ end
50
+
51
+ def create_text_dummy
52
+ "\"#{Faker::Lorem.sentence}\""
53
+ end
54
+
55
+ def create_integer_dummy
56
+ limit = extract_column_limit
57
+ limit_random = rand(1..limit)
58
+ Faker::Number.number(limit_random)
59
+ end
60
+
61
+ def create_float_dummy
62
+ limit = extract_column_limit
63
+ limit_random = rand(1..limit)
64
+ Faker::Number.decimal(limit_random)
65
+ end
66
+
67
+ def create_decimal_dummy
68
+ precision = extract_column_precision
69
+ scale = extract_column_scale
70
+ if scale.nil?
71
+ integer_part = rand(1..precision - 1)
72
+ decimal_part = precision - integer_part
73
+ else
74
+ decimal_part = scale
75
+ integer_part = precision - decimal_part
76
+ end
77
+ Faker::Number.decimal(integer_part, decimal_part)
78
+ end
79
+
80
+ def create_date_dummy
81
+ "\"Faker::Date.between(Date.today, Date.today)\""
82
+ end
83
+
84
+ def create_time_dummy
85
+ "\"#{Time.now.to_time_of_day}\""
86
+ end
87
+
88
+ def create_datetime_dummy
89
+ "\"#{Faker::Time.between(DateTime.now, DateTime.now)}\""
90
+ end
91
+
92
+ def create_boolean_dummy
93
+ Faker::Boolean.boolean
94
+ end
95
+
96
+ def input_file(dummy_data)
97
+ @seed_file.puts "#{@name}: #{dummy_data},"
98
+ end
99
+ end
100
+ end
data/lib/ittan/table.rb CHANGED
@@ -1,6 +1,9 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+
1
4
  module Ittan
2
5
  class Table
3
- attr_accessor :content, :name, :model_name, :fixtures_path, :seed_file_path
6
+ attr_accessor :content, :name, :model_name, :fixtures_path, :seed_file_path, :seed_file
4
7
 
5
8
  def initialize(table_sentence)
6
9
  # divide table_name from column
@@ -19,86 +22,31 @@ module Ittan
19
22
 
20
23
  def create_seed_file
21
24
  return if FileTest.exist?(@seed_file_path)
25
+
22
26
  begin
23
27
  File.open(@seed_file_path, "w") do |seed_file|
24
- seed_file.puts "#{@model_name}.seed(\n:id,\n{\n" # input file header
25
- @content[0].split("\n").each do |row|
26
- next unless row.include?("t.") # skip except column
27
- input_dummy_data(row, seed_file)
28
- end
29
- seed_file.puts "},\n)"
28
+ @seed_file = seed_file
29
+ input_seed_file
30
30
  end
31
- rescue SystemCallError => e
32
- puts %Q(class=[#{e.class}] message=[#{e.message}])
33
- rescue IOError => e
34
- puts %Q(class=[#{e.class}] message=[#{e.message}])
31
+
32
+ puts %Q(\e[32mcreate: #{@seed_file_path}\e[0m) # Create Message
33
+ rescue SystemCallError, IOError => e
34
+ puts %Q(\e[31mError: #{e.message}\e[0m) # Error Message
35
35
  end
36
36
  end
37
37
 
38
38
  private
39
39
 
40
- def input_dummy_data(column, file)
41
- column_name = column.match("\"[a-zA-Z0-9_]*\"")[0].delete("\"")
42
- file.puts "#{column_name}: #{create_string_dummy(column)}," if column.include?('t.string')
43
- file.puts "#{column_name}: #{create_text_dummy}," if column.include?('t.text')
44
- file.puts "#{column_name}: #{create_integer_dummy(column)}," if column.include?('t.integer')
45
- file.puts "#{column_name}: #{create_float_dummy(column)}," if column.include?('t.float')
46
- file.puts "#{column_name}: #{create_decimal_dummy(column)}," if column.include?('t.decimal')
47
- file.puts "#{column_name}: #{create_boolean_dummy}," if column.include?('t.boolean')
48
- file.puts "#{column_name}: #{create_date_dummy}," if column.include?('t.date ')
49
- file.puts "#{column_name}: #{create_time_dummy}," if column.include?('t.time')
50
- file.puts "#{column_name}: #{create_datetime_dummy}," if column.include?('t.datetime')
51
- end
52
-
53
- def create_string_dummy(column)
54
- limit = column.match("(limit: )[0-9]*") { |match_data| match_data[0].delete("^0-9").to_i }
55
- limit_random = rand(1..limit)
56
- "\"#{Faker::Lorem.characters(limit_random)}\""
57
- end
58
-
59
- def create_text_dummy
60
- "\"#{Faker::Lorem.sentence}\""
61
- end
62
-
63
- def create_integer_dummy(column)
64
- limit = column.match("(limit: )[0-9]*") { |match_data| match_data[0].delete("^0-9").to_i }
65
- limit_random = rand(1..limit)
66
- Faker::Number.number(limit_random)
67
- end
40
+ def input_seed_file
41
+ @seed_file.puts "#{@model_name}.seed(\n:id,\n{\n" # file header
68
42
 
69
- def create_float_dummy(column)
70
- limit = column.match("(limit: )[0-9]*") { |match_data| match_data[0].delete("^0-9").to_i }
71
- limit_random = rand(1..limit)
72
- Faker::Number.decimal(limit_random)
73
- end
74
-
75
- def create_decimal_dummy(column)
76
- precision = column.match("(precision: )[0-9]*") { |match_data| match_data[0].delete("^0-9").to_i }
77
- scale = column.match("(scale: )[0-9]*") { |match_data| match_data[0].delete("^0-9").to_i }
78
- if scale.nil?
79
- integer_part = rand(1..precision - 1)
80
- decimal_part = precision - integer_part
81
- else
82
- decimal_part = scale
83
- integer_part = precision - decimal_part
43
+ @content[0].split("\n").each do |column_text|
44
+ next unless column_text.include?("t.") # skip except column
45
+ column_instance = Ittan::Column.new(column_text, @seed_file)
46
+ column_instance.input_dummy_data
84
47
  end
85
- Faker::Number.decimal(integer_part, decimal_part)
86
- end
87
-
88
- def create_date_dummy
89
- "\"Faker::Date.between(Date.today, Date.today)\""
90
- end
91
-
92
- def create_time_dummy
93
- "\"#{Time.now.to_time_of_day}\""
94
- end
95
-
96
- def create_datetime_dummy
97
- "\"#{Faker::Time.between(DateTime.now, DateTime.now)}\""
98
- end
99
48
 
100
- def create_boolean_dummy
101
- Faker::Boolean.boolean
49
+ @seed_file.puts "},\n)" # file footer
102
50
  end
103
51
  end
104
52
  end
data/lib/ittan/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ittan
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ittan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - syossan27
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-07 00:00:00.000000000 Z
11
+ date: 2016-12-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faker
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: tod
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -117,6 +131,7 @@ files:
117
131
  - ittan.gemspec
118
132
  - lib/ittan.rb
119
133
  - lib/ittan/cli.rb
134
+ - lib/ittan/column.rb
120
135
  - lib/ittan/table.rb
121
136
  - lib/ittan/version.rb
122
137
  homepage: ''