scaffolding 0.2.9 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +17 -1
- data/lib/generators/scaffolding_generator.rb +10 -6
- data/lib/scaffolding/parser/base.rb +6 -5
- data/lib/scaffolding/parser/csv.rb +10 -8
- data/lib/scaffolding/parser/importer/csvdata.rb +1 -1
- data/lib/scaffolding/version.rb +1 -1
- data/lib/scaffolding.rb +17 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b8a09c0c41340385e00f79f271df39ee3633ee5
|
4
|
+
data.tar.gz: 1f3697d662598ede094f721f789568775a475e66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a7a4b55d969f4a318266fb3a377396f6b2c986726336a2757fc914f69c6097cb3b4c1caec65aaec478aa6ea07d2f46a31509e48db8db09b8f2ef1c66c0c0c17
|
7
|
+
data.tar.gz: 75f1ea1d54033388ea00a426bd20015141075df92d513939fdb5ca6ec3a62cbf8bd95f818cc2fceb97633356102de2ce8f07e38b2c544c3b53c5e7e654031314
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Scaffolding
|
2
2
|
|
3
|
-
Generate a rails scaffold from a CSV and import the data into your rails application.
|
3
|
+
Generate a rails scaffold from a CSV file and import the data into your rails application.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,12 +20,28 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
Ensure your file name is the same as the resource/model name you wish to generate.
|
24
|
+
|
23
25
|
$ rails g/generate scaffolding path/to/file.csv
|
24
26
|
|
25
27
|
Or add CSV files to the tmp/scaffolding directory and process them all with:
|
26
28
|
|
27
29
|
$ rails g/generate scaffolding
|
28
30
|
|
31
|
+
Options (You will be asked these at the terminal if you do not specify):
|
32
|
+
|
33
|
+
Automatically determine the data types of each column.
|
34
|
+
|
35
|
+
--auto
|
36
|
+
|
37
|
+
Migrate the database after the scaffold has been generated.
|
38
|
+
|
39
|
+
--migrate
|
40
|
+
|
41
|
+
Import the data from the data source into the development database.
|
42
|
+
|
43
|
+
--import
|
44
|
+
|
29
45
|
## Development
|
30
46
|
|
31
47
|
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.
|
@@ -4,15 +4,19 @@ Rails.application.load_tasks
|
|
4
4
|
require 'scaffolding'
|
5
5
|
|
6
6
|
class ScaffoldingGenerator < Rails::Generators::Base
|
7
|
-
|
7
|
+
desc "This generator generates a scaffold based on a CSV file and imports the data"
|
8
|
+
argument :source, :type => :string, :default => ""
|
9
|
+
class_option :auto, :type => :boolean, :default => false, :description => "Automatically choose data types"
|
10
|
+
class_option :migrate, :type => :boolean, :default => false, :description => "Migrate the database"
|
11
|
+
class_option :import, :type => :boolean, :default => false, :description => "Import data from source"
|
8
12
|
|
9
|
-
def
|
10
|
-
if
|
11
|
-
Dir[Rails.root.join('tmp/scaffolding/*')].each do |
|
12
|
-
Scaffolding.generate(
|
13
|
+
def analyize_sources
|
14
|
+
if source == "" || source.nil?
|
15
|
+
Dir[Rails.root.join('tmp/scaffolding/*')].each do |source|
|
16
|
+
Scaffolding.generate(source, options[:auto], options[:migrate], options[:import])
|
13
17
|
end
|
14
18
|
else
|
15
|
-
Scaffolding.generate(
|
19
|
+
Scaffolding.generate(source, options[:auto], options[:migrate], options[:import])
|
16
20
|
end
|
17
21
|
end
|
18
22
|
|
@@ -4,11 +4,12 @@ module Scaffolding
|
|
4
4
|
require 'rails'
|
5
5
|
require 'csv'
|
6
6
|
|
7
|
-
def initialize(file="")
|
7
|
+
def initialize(file="", auto)
|
8
8
|
@errors = []
|
9
9
|
@data = valid_data?(file)
|
10
10
|
@file_name = File.basename(file, ".*" ).to_s.split.join.camelize.strip.singularize
|
11
11
|
@scaffold_builder = @file_name
|
12
|
+
@auto = auto
|
12
13
|
@scaffolding = {}
|
13
14
|
end
|
14
15
|
|
@@ -49,17 +50,17 @@ module Scaffolding
|
|
49
50
|
row.each do |column, data|
|
50
51
|
data_type = :string
|
51
52
|
data_type = :boolean if ["true", "false"].include?(data.to_s.downcase) rescue data_type
|
52
|
-
data_type = :date if Date.parse(Date.strptime(data, '%m/%d/%Y')) rescue data_type
|
53
|
+
data_type = :date if Date.parse(Date.strptime(data, '%m/%d/%Y')) || Date.parse(Date.strptime(data, '%d/%m/%Y')) rescue data_type
|
53
54
|
data_type = :time if Time.parse(Time.strptime(data, '%H:%M:%S')) rescue data_type
|
54
|
-
data_type = :datetime if DateTime.parse(DateTime.strptime(data, '%m/%d/%Y %H:%M:%S')) rescue data_type
|
55
|
+
data_type = :datetime if DateTime.parse(DateTime.strptime(data, '%m/%d/%Y %H:%M:%S')) || DateTime.parse(DateTime.strptime(data, '%d/%m/%Y %H:%M:%S')) rescue data_type
|
55
56
|
data_type = :integer if Integer(data) rescue data_type
|
56
57
|
data_type = :decimal if ((data.to_f - data.to_i).abs > 0.0) rescue data_type
|
57
58
|
@scaffolding[column.to_sym][data_type] += 1 unless data == ""
|
58
59
|
end
|
59
60
|
end
|
60
61
|
|
61
|
-
def self.process(file)
|
62
|
-
importer = self.new(file)
|
62
|
+
def self.process(file, auto)
|
63
|
+
importer = self.new(file, auto)
|
63
64
|
return importer.errors unless importer.errors.count == 0
|
64
65
|
importer.results
|
65
66
|
end
|
@@ -2,7 +2,7 @@ module Scaffolding
|
|
2
2
|
module Parser
|
3
3
|
class Csv < Scaffolding::Parser::Base
|
4
4
|
|
5
|
-
def initialize(file = "")
|
5
|
+
def initialize(file = "", auto)
|
6
6
|
super
|
7
7
|
@headers = true
|
8
8
|
@row_number = 0
|
@@ -26,15 +26,17 @@ module Scaffolding
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def scaffold_rank
|
29
|
-
|
30
|
-
|
29
|
+
unless @auto
|
30
|
+
puts "\n\e[33mManually choose data types?(y/n)\e[0m"
|
31
|
+
manual = STDIN.gets.chomp
|
32
|
+
end
|
31
33
|
@scaffolding.each do |scaffold, data_types|
|
32
34
|
data_type = data_types.max_by{|k,v| v}[0]
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
unless @auto || manual != "y"
|
36
|
+
puts "\n\e[32m#{scaffold}\e[0m is a \e[33m#{data_type}\e[0m? (y/string/integer/date ect)"
|
37
|
+
answer = STDIN.gets.chomp.downcase
|
38
|
+
data_type = answer unless answer == "y" || answer == ""
|
39
|
+
end
|
38
40
|
@scaffolding[scaffold] = data_type
|
39
41
|
end
|
40
42
|
end
|
data/lib/scaffolding/version.rb
CHANGED
data/lib/scaffolding.rb
CHANGED
@@ -3,12 +3,14 @@ require "scaffolding/version"
|
|
3
3
|
module Scaffolding
|
4
4
|
if defined?(Rails)
|
5
5
|
require 'scaffolding/railtie'
|
6
|
-
# Dir.mkdir(
|
6
|
+
# Dir.mkdir(source.join(Rails.root, 'tmp/scaffolding'))
|
7
7
|
end
|
8
8
|
|
9
|
-
def self.generate(
|
10
|
-
@
|
11
|
-
|
9
|
+
def self.generate(source, auto, migrate, import)
|
10
|
+
@source = source
|
11
|
+
@migrate = migrate
|
12
|
+
@import = import
|
13
|
+
results = Scaffolding::Parser::Csv.process(@source, auto)
|
12
14
|
return if Scaffolding.errors(results)
|
13
15
|
Rails::Generators::Base.new.generate "scaffold", results
|
14
16
|
Scaffolding.import_data if Scaffolding.migrate_database
|
@@ -24,8 +26,11 @@ module Scaffolding
|
|
24
26
|
end
|
25
27
|
|
26
28
|
def self.migrate_database
|
27
|
-
|
28
|
-
|
29
|
+
unless @migrate
|
30
|
+
puts "\n\n\e[32mMigrate the database?(y/n)\e[0m\n"
|
31
|
+
answer = STDIN.gets.chomp.downcase
|
32
|
+
end
|
33
|
+
if @migrate || answer == "y"
|
29
34
|
Rake::Task["db:migrate"].invoke
|
30
35
|
else
|
31
36
|
false
|
@@ -33,9 +38,12 @@ module Scaffolding
|
|
33
38
|
end
|
34
39
|
|
35
40
|
def self.import_data
|
36
|
-
|
37
|
-
|
38
|
-
|
41
|
+
unless @import
|
42
|
+
puts "\n\n\e[32mImport the data from #{@source}?(y/n)\e[0m\n"
|
43
|
+
answer = STDIN.gets.chomp.downcase
|
44
|
+
end
|
45
|
+
if @import || answer == "y"
|
46
|
+
Scaffolding::Parser::Importer::CsvData.process(@source, false).each do |k,v|
|
39
47
|
puts "#{v} records #{k}"
|
40
48
|
end
|
41
49
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scaffolding
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan-deJong
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|