schema_to_scaffold 0.7.2 → 0.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 33d0eccf2bd2dc8b292cf77be785d91a7a151b24
4
- data.tar.gz: 2f6ad8537ef8dbe25627816970b8251d595921b2
3
+ metadata.gz: 2687f54c3d084dbb6678d431c66db92a31ba691c
4
+ data.tar.gz: f731ebdb43c91ba5f8e98f0c3a26507aeaf8da61
5
5
  SHA512:
6
- metadata.gz: 8658f91b032f863355d9a2644b76554d90dc3c2aae38c0b2c5b7876a1a2c9cc087b4d88e8ca32ddc8eb8e750160905511ea0e57a702e1036a659d87215b44039
7
- data.tar.gz: 74fc31c2e35076c176f0700dc6804d6a2b1a0466895a1c8d76522b588bc99e59a7746daa11ac98b776f0e2e45cf8ebed0e3909f44ff8186defdb4f1a5bf8eb7a
6
+ metadata.gz: 733cce3b7f62c783a43a095e1c339411416c0a3a81664d06d77120b1945d502960dc8213d3e6df61fa9670bba5c8d823ed9918e349fa6c48c50ef42125cff27c
7
+ data.tar.gz: c16a1b76fc4841df4025e71ab8d781acb7c29687f1b59008c3113f1743cd55be9f02e4fa58ec24c55114c2e87b38b75c65efbf2b9a4ae2d67ef374c4c258f755
data/.gitignore CHANGED
@@ -17,4 +17,5 @@ test/version_tmp
17
17
  tmp
18
18
  .project
19
19
  /schema.rb
20
- .idea
20
+ .idea
21
+ spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1 @@
1
+ schema-to-scaffold
@@ -1 +1 @@
1
- 2.1.1@schema-to-scaffold
1
+ 2.3.1
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -1,77 +1,12 @@
1
1
  #!/usr/bin/env ruby
2
- $:.unshift File.expand_path('../lib', File.dirname(__FILE__))
3
- require 'schema_to_scaffold'
4
- ## Argument conditions
2
+ # encoding: UTF-8
5
3
 
6
- opts = SchemaToScaffold.parse_arguments(ARGV)
7
- ARGV.clear
4
+ # resolve bin path, ignoring symlinks
5
+ require "pathname"
6
+ bin_file = Pathname.new(__FILE__).realpath
7
+ # add self to libpath
8
+ $:.unshift File.expand_path("../../lib", bin_file)
8
9
 
9
- if opts[:help]
10
- puts SchemaToScaffold.help_msg
11
- exit 0
12
- end
10
+ require 'schema_to_scaffold/cli'
13
11
 
14
- ## looking for /schema\S*.rb$/ in user directory
15
- paths = SchemaToScaffold::Path.new(opts[:path])
16
- path = paths.choose unless opts[:path].to_s.match(/\.rb$/)
17
-
18
- ## Opening file
19
- path||=opts[:path]
20
- begin
21
- data = File.open(path, 'r') {|f| f.read }
22
- rescue
23
- puts "\nUnable to open file '#{path}'"
24
- exit 1
25
- end
26
-
27
- ## Generate scripts from schema
28
-
29
- schema = SchemaToScaffold::Schema.new(data)
30
-
31
- begin
32
- raise if schema.table_names.empty?
33
- puts "\nLoaded tables:"
34
- schema.table_names.each_with_index {|name,i| puts "#{i}. #{name}" }
35
-
36
- puts "\nOptions are:\n4 for table 4; (4..6) for table 4 to 6; [4,6] for tables 4 and 6; * for all Tables"
37
-
38
- print "\nSelect a table: "
39
-
40
- rescue
41
- puts "Could not find tables in '#{path}'"
42
- exit 1
43
- end
44
-
45
- result = gets.strip
46
- begin
47
- case result
48
- when "*"
49
- tables = (0..schema.table_names.count-1).map{|i|i}
50
- when /^\d/
51
- tables = [result.to_i]
52
- else
53
- tables = eval(result).to_a.map{|i|i}
54
- end
55
- raise if tables.empty?
56
- rescue Exception => e
57
- puts "Not a valid input, \nOptions are:\n4 for table 4; (4..6) for table 4 to 6; [4,6] for tables 4 and 6; * for all Tables"
58
- exit 1
59
- end
60
-
61
- script = []
62
- target = opts[:factory_girl] ? "factory_girl:model" : "scaffold"
63
- migragion_flag = opts[:migration] || opts[:factory_girl]
64
- tables.each { |table_id| script << SchemaToScaffold.generate_script(schema, table_id, target, migragion_flag) }
65
- output = script.join("")
66
- puts "\nScript for #{target}:\n\n"
67
- puts output
68
-
69
- if opts[:clipboard]
70
- puts("\n(copied to your clipboard)")
71
- case RUBY_PLATFORM
72
- when /darwin/i then exec("echo '#{output}' | tr -d '\n' | pbcopy")
73
- when /linux/i then exec("echo '#{output}' | tr -d '\n' | xclip -selection c")
74
- when /mingw/i then exec("echo '#{output}' | clip")
75
- when /win/i then exec("echo '#{output}' | clip")
76
- end
77
- end
12
+ SchemaToScaffold::CLI.start(*ARGV)
@@ -3,84 +3,9 @@ require 'schema_to_scaffold/schema'
3
3
  require 'schema_to_scaffold/table'
4
4
  require 'schema_to_scaffold/attribute'
5
5
  require 'schema_to_scaffold/path'
6
+ require 'schema_to_scaffold/cli'
7
+ require 'schema_to_scaffold/help'
8
+ require 'schema_to_scaffold/clipboard'
6
9
 
7
10
  module SchemaToScaffold
8
- extend self
9
-
10
- ## Usage help text to print in all platforms
11
-
12
- GENERIC_HELP = <<-END_OF_HELP
13
-
14
- Usage: scaffold [options]
15
- Generate a rails scaffold script for a given schema.rb
16
- -h Displays help.
17
- -p <path> It specifies a path to a folder or to a file.
18
- -c Will copy the script to your clipboard. Requires xclip be installed on Linux.
19
- -f Generates a factory_girl:model rather than a full scaffold.
20
- -m Add migration (use if your schema comes from a different database)
21
-
22
- END_OF_HELP
23
-
24
-
25
- ## Windows specific usage help text
26
-
27
- WINDOWS_HELP = <<-WINDOWS_SAMPLE
28
- Examples:
29
- scaffold
30
- scaffold -p C:\\Users\\JohnDoe
31
- scaffold -c -p C:\\Users\\JohnDoe\\Documents\\schema.rb
32
- WINDOWS_SAMPLE
33
-
34
- ## Linux specific usage help text
35
-
36
- LINUX_HELP = <<-LINUX_SAMPLE
37
- Examples:
38
- scaffold
39
- scaffold -c -p ~/work/rails/my_app
40
- scaffold -c -p ~/work/rails/my_app/db/schema.rb
41
- LINUX_SAMPLE
42
-
43
- def help_msg
44
- return GENERIC_HELP +
45
- case RUBY_PLATFORM
46
- when /darwin/i then LINUX_HELP
47
- when /linux/i then LINUX_HELP
48
- when /mingw/i then WINDOWS_HELP
49
- when /win/i then WINDOWS_HELP
50
- end
51
- end
52
-
53
- ##
54
- # Parses ARGV and returns a hash of options.
55
-
56
- def parse_arguments(argv)
57
- if argv_index = argv.index("-p")
58
- path = argv.delete_at(argv_index+1)
59
- argv.delete('-p')
60
- end
61
- args = {
62
- :clipboard => argv.delete('-c'), # check for clipboard flag
63
- :factory_girl => argv.delete('-f'), # factory_girl instead of scaffold
64
- :migration => argv.delete('-m'), # generate migrations
65
- :help => argv.delete('-h'), # check for help flag
66
- :path => path # get path to file(s)
67
- }
68
- unless argv.empty?
69
- puts "\n------\nWrong set of arguments.\n------\n"
70
- puts help_msg
71
- exit
72
- else
73
- args
74
- end
75
-
76
- end
77
-
78
- ##
79
- # Generates the rails scaffold script
80
-
81
- def self.generate_script(schema, table=nil,target,migragion_flag)
82
- schema = Schema.new(schema) unless schema.is_a? Schema
83
- return schema.to_script if table.nil?
84
- schema.table(table).to_script target, migragion_flag
85
- end
86
11
  end
@@ -8,18 +8,17 @@ module SchemaToScaffold
8
8
  end
9
9
 
10
10
  def to_script
11
- "#{name}:#{type}" unless ["created_at","updated_at"].include? name
11
+ "#{name}:#{type}" unless ["created_at","updated_at"].include?(name)
12
12
  end
13
13
 
14
14
  def self.parse(attribute)
15
15
  match = attribute.match(/t\.(\w+)\s+"(\w+)"/)
16
16
  if match
17
- name = match.captures[1].sub(/_id$/,'')
17
+ name = match.captures[1].sub(/_id$/, "")
18
18
  type = $&.nil? ? match.captures[0] : "references"
19
- Attribute.new(name,type)
19
+ Attribute.new(name, type)
20
20
  end
21
21
  end
22
22
 
23
-
24
23
  end
25
24
  end
@@ -0,0 +1,103 @@
1
+ require "schema_to_scaffold"
2
+ module SchemaToScaffold
3
+ class CLI
4
+
5
+ TABLE_OPTIONS = "\nOptions are:\n4 for table 4; (4..6) for table 4 to 6; [4,6] for tables 4 and 6; * for all Tables"
6
+
7
+ def self.start(*args)
8
+ ## Argument conditions
9
+ opts = parse_arguments(args)
10
+
11
+ if opts[:help]
12
+ puts Help.message
13
+ exit 0
14
+ end
15
+
16
+ ## looking for /schema\S*.rb$/ in user directory
17
+ paths = Path.new(opts[:path])
18
+ path = paths.choose unless opts[:path].to_s.match(/\.rb$/)
19
+
20
+ ## Opening file
21
+ path ||= opts[:path]
22
+ begin
23
+ data = File.open(path, 'r') { |f| f.read }
24
+ rescue
25
+ puts "\nUnable to open file '#{path}'"
26
+ exit 1
27
+ end
28
+
29
+ ## Generate scripts from schema
30
+ schema = Schema.new(data)
31
+
32
+ begin
33
+ raise if schema.table_names.empty?
34
+ puts "\nLoaded tables:"
35
+ schema.print_table_names
36
+ puts TABLE_OPTIONS
37
+ print "\nSelect a table: "
38
+ rescue
39
+ puts "Could not find tables in '#{path}'"
40
+ exit 1
41
+ end
42
+
43
+ input = STDIN.gets.strip
44
+ begin
45
+ tables = schema.select_tables(input)
46
+ raise if tables.empty?
47
+ rescue
48
+ puts "Not a valid input. #{TABLE_OPTIONS}"
49
+ exit 1
50
+ end
51
+
52
+ script = []
53
+ target = opts[:factory_girl] ? "factory_girl:model" : "scaffold"
54
+ migration_flag = opts[:migration] ? true : false
55
+
56
+ tables.each do |table_id|
57
+ script << generate_script(schema, table_id, target, migration_flag)
58
+ end
59
+ output = script.join("")
60
+ puts "\nScript for #{target}:\n\n"
61
+ puts output
62
+
63
+ if opts[:clipboard]
64
+ puts("\n(copied to your clipboard)")
65
+ Clipboard.new(output).command
66
+ end
67
+ end
68
+
69
+ ##
70
+ # Parses ARGV and returns a hash of options.
71
+ def self.parse_arguments(argv)
72
+ if argv_index = argv.index("-p")
73
+ path = argv.delete_at(argv_index + 1)
74
+ argv.delete('-p')
75
+ end
76
+
77
+ args = {
78
+ clipboard: argv.delete('-c'), # check for clipboard flag
79
+ factory_girl: argv.delete('-f'), # factory_girl instead of scaffold
80
+ migration: argv.delete('-m'), # generate migrations
81
+ help: argv.delete('-h'), # check for help flag
82
+ path: path # get path to file(s)
83
+ }
84
+
85
+ if argv.empty?
86
+ args
87
+ else
88
+ puts "\n------\nWrong set of arguments.\n------\n"
89
+ puts Help.message
90
+ exit
91
+ end
92
+ end
93
+
94
+ ##
95
+ # Generates the rails scaffold script
96
+ def self.generate_script(schema, table=nil, target, migration_flag)
97
+ schema = Schema.new(schema) unless schema.is_a?(Schema)
98
+ return schema.to_script if table.nil?
99
+ schema.table(table).to_script(target, migration_flag)
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,36 @@
1
+ module SchemaToScaffold
2
+ class Clipboard
3
+ attr_reader :output
4
+
5
+ def initialize(output)
6
+ @output = output
7
+ end
8
+
9
+ def command
10
+ case platform
11
+ when /darwin/i then darwin_command
12
+ when /linux/i then linux_command
13
+ when /mingw/i then win_command
14
+ when /win/i then win_command
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def darwin_command
21
+ exec("echo '#{output}' | tr -d '\n' | pbcopy")
22
+ end
23
+
24
+ def linux_command
25
+ exec("echo '#{output}' | tr -d '\n' | xclip -selection c")
26
+ end
27
+
28
+ def win_command
29
+ exec("echo '#{output}' | clip")
30
+ end
31
+
32
+ def platform
33
+ RUBY_PLATFORM
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,46 @@
1
+ module SchemaToScaffold
2
+ class Help
3
+ ## Usage help text to print in all platforms
4
+ GENERIC_HELP = <<-END_OF_HELP
5
+
6
+ Usage: scaffold [options]
7
+ Generate a rails scaffold script for a given schema.rb
8
+ -h Displays help.
9
+ -p <path> It specifies a path to a folder or to a file.
10
+ -c Will copy the script to your clipboard. Requires xclip be installed on Linux.
11
+ -f Generates a factory_girl:model rather than a full scaffold.
12
+ -m Add migration (use if your schema comes from a different database)
13
+
14
+ END_OF_HELP
15
+
16
+ ## Windows specific usage help text
17
+ WINDOWS_HELP = <<-WINDOWS_SAMPLE
18
+ Examples:
19
+ scaffold
20
+ scaffold -p C:\\Users\\JohnDoe
21
+ scaffold -c -p C:\\Users\\JohnDoe\\Documents\\schema.rb
22
+ WINDOWS_SAMPLE
23
+
24
+ ## Linux specific usage help text
25
+ LINUX_HELP = <<-LINUX_SAMPLE
26
+ Examples:
27
+ scaffold
28
+ scaffold -c -p ~/work/rails/my_app
29
+ scaffold -c -p ~/work/rails/my_app/db/schema.rb
30
+ LINUX_SAMPLE
31
+
32
+ def self.message
33
+ return GENERIC_HELP +
34
+ case platform
35
+ when /darwin/i then LINUX_HELP
36
+ when /linux/i then LINUX_HELP
37
+ when /mingw/i then WINDOWS_HELP
38
+ when /win/i then WINDOWS_HELP
39
+ end
40
+ end
41
+
42
+ def self.platform
43
+ RUBY_PLATFORM
44
+ end
45
+ end
46
+ end
@@ -1,54 +1,54 @@
1
1
  require 'find'
2
2
  module SchemaToScaffold
3
-
4
3
  ##
5
4
  # Deal with the path argument
6
5
 
7
6
  class Path
8
-
7
+
9
8
  def initialize(path)
10
- @search_path = Dir.pwd
11
- @path = path
9
+ @path = path || Dir.pwd
12
10
  end
13
11
 
14
12
  ##
15
- # Validate if a given path leads to a directory
16
- def check_directory
17
- unless File.directory?(@search_path)
18
- puts "\nSorry #{@search_path} is not a valid directory!\nHere is an example:\nscaffold -p /home/foo/bar"
13
+ # Return the chosen path
14
+ def choose
15
+ validate_path
16
+ search_paths_list = search_paths
17
+ if search_paths_list.empty?
18
+ puts "\nThere is no /schema[^\/]*.rb$/ in the directory #{@path}"
19
19
  exit
20
20
  end
21
- puts "\nLooking for schema.rb in #{@search_path}"
21
+
22
+ search_paths_list.each_with_index {|path,indx| puts "#{indx}. #{path}" }
23
+
24
+ begin
25
+ print "\nSelect a path to the target schema: "
26
+ end while search_paths_list[(id = STDIN.gets.to_i)].nil?
27
+
28
+ search_paths_list[id]
22
29
  end
23
-
24
30
 
31
+ private
25
32
  ##
26
- # Return the chosen path
27
- def choose
28
- search_rb
29
- if @schema_paths.empty?
30
- puts "\nSorry there is none /schema[^\/]*.rb$/ in the directory #{@search_path}"
33
+ # Validate if a given path leads to a directory
34
+ def validate_path
35
+ if File.directory?(@path.to_s)
36
+ puts "\nLooking for schema.rb in #{@path}"
37
+ else
38
+ puts "\nSorry #{@path} is not a valid directory!\nHere is an example:\nscaffold -p /home/foo/bar"
31
39
  exit
32
40
  end
33
- @schema_paths.each_with_index {|path,indx| puts "#{indx}. #{path}" }
34
- begin
35
- print "\nSelect a path to the target schema: "
36
- end while @schema_paths[(id = gets.to_i)].nil?
37
- @schema_paths[id]
38
41
  end
39
42
 
40
- private
41
43
  ##
42
44
  # Will search for /schema[^\/]*.rb$/ in the current directory
43
- def search_rb
44
- @search_path = @path.to_s unless @path.nil?
45
- check_directory
46
- @schema_paths = Array.new
47
- Find.find(@search_path) do |s_p|
48
- @schema_paths<<s_p if s_p[/schema[^\/]*.rb$/]
45
+ def search_paths
46
+ result = []
47
+ Find.find(@path) do |s_p|
48
+ result << s_p if s_p[/schema[^\/]*.rb$/]
49
49
  end
50
+ result
50
51
  end
51
52
 
52
53
  end
53
-
54
54
  end
@@ -11,6 +11,19 @@ module SchemaToScaffold
11
11
  tables.map(&:name)
12
12
  end
13
13
 
14
+ def print_table_names
15
+ table_names.each_with_index { |name, i| puts "#{i}. #{name}" }
16
+ end
17
+
18
+ def select_tables(input)
19
+ case input
20
+ when "*"
21
+ table_range.to_a
22
+ when /^\d/
23
+ table_range.include?(input.to_i) ? [input.to_i] : []
24
+ end
25
+ end
26
+
14
27
  def table(id)
15
28
  case id
16
29
  when Symbol then table(id.to_s)
@@ -25,7 +38,13 @@ module SchemaToScaffold
25
38
  end
26
39
 
27
40
  def self.parse(data)
28
- data.split(/^\s*create_/)[1..-1].map {|table_data| Table.parse table_data }
41
+ data.split(/^\s*create_/)[1..-1].map {|table_data| Table.parse(table_data) }.reject{ |e| e.nil? }
42
+ end
43
+
44
+ private
45
+
46
+ def table_range
47
+ 0...table_names.count
29
48
  end
30
49
  end
31
50
  end
@@ -11,31 +11,37 @@ module SchemaToScaffold
11
11
  @name, @attributes = name, attributes
12
12
  end
13
13
 
14
- def to_script(target, migragion_flag)
14
+ def to_script(target, migration_flag)
15
15
  begin
16
16
  attributes_list = attributes.map(&:to_script).reject { |x| x.nil? || x.empty? }.join(' ')
17
- rescue Exception => e
17
+ rescue => e
18
18
  puts "\n ---------------------------------------------"
19
19
  puts e.message
20
20
  puts "Table \n\n\n #{self.inspect} \n\n\n"
21
21
  puts "\n ---------------------------------------------"
22
22
  end
23
23
  script = []
24
- script << "rails generate #{target} #{modelize name} #{attributes_list}"
25
- script << " --no-migration" unless migragion_flag
24
+ script << "rails generate #{target} #{modelize(name)} #{attributes_list}"
25
+ script << " --no-migration" unless migration_flag
26
26
  script << "\n\n"
27
- return script
27
+ script
28
28
  end
29
29
 
30
30
  def self.parse(table_data)
31
31
  return unless name = table_data[/table "([^"]+?)"/]
32
32
  name = $1
33
- atts = table_data.lines.to_a.select { |line| line =~ /t\.\w+/ }.map { |att| Attribute.parse att }
34
- Table.new(name, atts)
33
+ table_fields = table_fields_of(table_data)
34
+ Table.new(name, table_fields)
35
35
  end
36
36
 
37
37
  private
38
- def modelize (string)
38
+ def self.table_fields_of(table_data)
39
+ table_data.lines.to_a.select { |line| line =~ /t\.(?!index)\w+/ }.map { |att| Attribute.parse(att) }
40
+ end
41
+
42
+ private
43
+
44
+ def modelize(string)
39
45
  string.camelize.singularize
40
46
  end
41
47
 
@@ -1,6 +1,6 @@
1
1
  module SchemaToScaffold
2
2
  MAJOR = 0
3
- MINOR = 7
4
- REVISION = 2
3
+ MINOR = 8
4
+ REVISION = 0
5
5
  VERSION = [MAJOR, MINOR, REVISION].join('.')
6
6
  end
@@ -17,10 +17,11 @@ EOD
17
17
  gem.files = `git ls-files`.split($/)
18
18
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
19
  gem.require_paths = ["lib"]
20
- gem.post_install_message = "Thanks for installing!"
21
20
  gem.licenses = ['MIT']
22
21
  gem.required_ruby_version = '>= 1.9.3'
23
22
 
24
23
  gem.add_runtime_dependency('activesupport', '>= 3.2.1')
25
24
  gem.add_development_dependency('pry', '~> 0.10', '>= 0.10.0')
25
+ gem.add_development_dependency('rspec')
26
+ gem.add_development_dependency('simplecov')
26
27
  end
@@ -0,0 +1,70 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Attribute do
3
+
4
+ describe ".parse" do
5
+ it "parses _id attribute to references" do
6
+ attribute = Attribute.parse('t.integer "user_id"')
7
+ expect(attribute.type).to eq("references")
8
+ expect(attribute.name).to eq("user")
9
+ end
10
+
11
+ it "parses string attributes" do
12
+ attribute = Attribute.parse('t.string "filename"')
13
+ expect(attribute.type).to eq("string")
14
+ expect(attribute.name).to eq("filename")
15
+ end
16
+
17
+ it "parses datetime attributes" do
18
+ attribute = Attribute.parse('t.datetime "created_at", null: false')
19
+ expect(attribute.type).to eq("datetime")
20
+ expect(attribute.name).to eq("created_at")
21
+ end
22
+
23
+ it "parses limit value" do
24
+ pending
25
+ attribute = Attribute.parse('t.integer "quota", limit: 8')
26
+ expect(attribute.type).to eq("integer")
27
+ expect(attribute.name).to eq("quota")
28
+ expect(attribute.limit).to eq("8")
29
+ end
30
+
31
+ it "parses decimals precision and scale values" do
32
+ pending
33
+ attribute = Attribute.parse('t.decimal "price", precision: 20, scale: 2, default: 0.0')
34
+ expect(attribute.type).to eq("decimal")
35
+ expect(attribute.name).to eq("price")
36
+ expect(attribute.precision).to eq("20")
37
+ expect(attribute.scale).to eq("2")
38
+ end
39
+ end
40
+
41
+ describe "#to_script" do
42
+ it "returns name and type" do
43
+ attribute = Attribute.parse('t.string "filename"')
44
+ expect(attribute.to_script).to eq("filename:string")
45
+ end
46
+
47
+ it "ignores updated_at" do
48
+ attribute = Attribute.parse('t.datetime "updated_at", null: false')
49
+ expect(attribute.to_script).to be_nil
50
+ end
51
+
52
+ it "ignores created_at" do
53
+ attribute = Attribute.parse('t.datetime "created_at", null: false')
54
+ expect(attribute.to_script).to be_nil
55
+ end
56
+
57
+ it "adds limit value to integer" do
58
+ pending
59
+ attribute = Attribute.parse('t.integer "quota", limit: 8')
60
+ expect(attribute.to_script).to eq("quota:integer{8}")
61
+ end
62
+
63
+ it "adds precision and scale value to decimal" do
64
+ pending
65
+ attribute = Attribute.parse('t.decimal "price", precision: 20, scale: 2, default: 0.0')
66
+ expect(attribute.to_script).to eq("price:decimal{20,2}")
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,116 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe CLI do
3
+
4
+ let(:empty_schema_file) { File.expand_path(File.dirname(__FILE__)) + '/support/empty_schema.rb' }
5
+ let(:schema_file) { File.expand_path(File.dirname(__FILE__)) + '/support/schema.rb' }
6
+
7
+ describe ".start" do
8
+ it "prints help message" do
9
+ expect {
10
+ begin
11
+ SchemaToScaffold::CLI.start("-h")
12
+ rescue SystemExit
13
+ end
14
+ }.to output(/Usage:/).to_stdout
15
+ end
16
+
17
+ it "prints file not found message" do
18
+ expect {
19
+ begin
20
+ SchemaToScaffold::CLI.start("-p", "/support/schema.rb")
21
+ rescue SystemExit
22
+ end
23
+ }.to output(/Unable to open file /).to_stdout
24
+ end
25
+
26
+ it "prints could not find tables message" do
27
+ expect {
28
+ begin
29
+ SchemaToScaffold::CLI.start("-p", empty_schema_file)
30
+ rescue SystemExit
31
+ end
32
+ }.to output(/Could not find tables /).to_stdout
33
+ end
34
+
35
+ context "choose table" do
36
+ before do
37
+ allow(STDIN).to receive(:gets) { "" }
38
+ end
39
+
40
+ it "prints Not a valid input message" do
41
+ expect {
42
+ begin
43
+ SchemaToScaffold::CLI.start("-p", schema_file)
44
+ rescue SystemExit
45
+ end
46
+ }.to output(/Not a valid input/).to_stdout
47
+ end
48
+ end
49
+
50
+ context "choose all tables" do
51
+ before do
52
+ allow(STDIN).to receive(:gets).once { "*" }
53
+ end
54
+
55
+ it "prints scaffold lines message" do
56
+ expect {
57
+ begin
58
+ SchemaToScaffold::CLI.start("-p", schema_file)
59
+ rescue SystemExit
60
+ end
61
+ }.to output(/Script for/).to_stdout
62
+ end
63
+ end
64
+
65
+ context "copies first table to clipboard" do
66
+ before do
67
+ allow(STDIN).to receive(:gets).once { "0" }
68
+ expect_any_instance_of(Kernel).to receive(:exec).with("echo 'rails generate scaffold User email:string encrypted_password:string reset_password_token:string reset_password_sent_at:datetime remember_created_at:datetime sign_in_count:integer current_sign_in_at:datetime last_sign_in_at:datetime current_sign_in_ip:string last_sign_in_ip:string name:string role:integer --no-migration\n\n' | tr -d '\n' | pbcopy")
69
+ expect_any_instance_of(Clipboard).to receive(:platform).and_return("/darwin/i")
70
+ end
71
+
72
+ it "prints copied to your clipboard message" do
73
+ expect {
74
+ begin
75
+ SchemaToScaffold::CLI.start("-c", "-p", schema_file)
76
+ rescue SystemExit
77
+ end
78
+ }.to output(/copied to your clipboard/).to_stdout
79
+ end
80
+ end
81
+ end
82
+
83
+ describe ".parse_arguments" do
84
+ it "handles wrong arguments" do
85
+ expect {
86
+ begin
87
+ SchemaToScaffold::CLI.parse_arguments(["--help"])
88
+ rescue SystemExit
89
+ end
90
+ }.to output(/Wrong set of arguments/).to_stdout
91
+ end
92
+
93
+ context "handles arguments" do
94
+ it "help argument" do
95
+ expect(CLI.parse_arguments(["-h"])).to eq({clipboard: nil, factory_girl: nil, migration: nil, help: "-h", path: nil})
96
+ end
97
+
98
+ it "path argument" do
99
+ expect(CLI.parse_arguments(["-p", "/some/path"])).to eq({clipboard: nil, factory_girl: nil, migration: nil, help: nil, path: "/some/path"})
100
+ end
101
+
102
+ it "clipboard argument" do
103
+ expect(CLI.parse_arguments(["-c"])).to eq({clipboard: "-c", factory_girl: nil, migration: nil, help: nil, path: nil})
104
+ end
105
+
106
+ it "factory girl argument" do
107
+ expect(CLI.parse_arguments(["-f"])).to eq({clipboard: nil, factory_girl: "-f", migration: nil, help: nil, path: nil})
108
+ end
109
+
110
+ it "migration argument" do
111
+ expect(CLI.parse_arguments(["-m"])).to eq({clipboard: nil, factory_girl: nil, migration: "-m", help: nil, path: nil})
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,52 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Clipboard do
3
+ let(:output) { "rails g model ....." }
4
+ let(:clipboard) { Clipboard.new(output) }
5
+
6
+ describe "#command" do
7
+ context "darwin" do
8
+ before do
9
+ allow(clipboard).to receive(:platform).and_return("darwin")
10
+ expect_any_instance_of(Kernel).to receive(:exec).with("echo 'rails g model .....' | tr -d '\n' | pbcopy")
11
+ end
12
+
13
+ it "executes darwin command" do
14
+ clipboard.command
15
+ end
16
+ end
17
+
18
+ context "linux" do
19
+ before do
20
+ allow(clipboard).to receive(:platform).and_return("linux")
21
+ expect_any_instance_of(Kernel).to receive(:exec).with("echo 'rails g model .....' | tr -d '\n' | xclip -selection c")
22
+ end
23
+
24
+ it "executes linux command" do
25
+ clipboard.command
26
+ end
27
+ end
28
+
29
+ context "windows (win)" do
30
+ before do
31
+ allow(clipboard).to receive(:platform).and_return("win")
32
+ expect_any_instance_of(Kernel).to receive(:exec).with("echo 'rails g model .....' | clip")
33
+ end
34
+
35
+ it "executes windows command" do
36
+ clipboard.command
37
+ end
38
+ end
39
+
40
+ context "windows (mingw)" do
41
+ before do
42
+ allow(clipboard).to receive(:platform).and_return("mingw")
43
+ expect_any_instance_of(Kernel).to receive(:exec).with("echo 'rails g model .....' | clip")
44
+ end
45
+
46
+ it "executes windows command" do
47
+ clipboard.command
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,25 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Help do
3
+ describe ".message" do
4
+ it "returns message for darwin platform" do
5
+ allow(Help).to receive(:platform).and_return("darwin")
6
+ expect(Help.message).to include("work/rails/my_app/")
7
+ end
8
+
9
+ it "returns message for linux platform" do
10
+ allow(Help).to receive(:platform).and_return("linux")
11
+ expect(Help.message).to include("work/rails/my_app/")
12
+ end
13
+
14
+ it "returns message for windows(win) platform" do
15
+ allow(Help).to receive(:platform).and_return("win")
16
+ expect(Help.message).to include("JohnDoe\\Documents")
17
+ end
18
+
19
+ it "returns message for windows (mingw) platform" do
20
+ allow(Help).to receive(:platform).and_return("mingw")
21
+ expect(Help.message).to include("JohnDoe\\Documents")
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,41 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Path do
3
+
4
+ describe "#choose" do
5
+ it "prints error when given path is not an directory" do
6
+ path = Path.new("path_spec.rb")
7
+ expect {
8
+ begin
9
+ path.choose
10
+ rescue SystemExit
11
+ end
12
+ }.to output(/Sorry path_spec\.rb is not a valid directory!/).to_stdout
13
+ end
14
+
15
+ it "prints message when given path is a directory but no schema is found" do
16
+ path = Path.new(File.expand_path(File.dirname(__FILE__)) + '/support/no_schema')
17
+ expect {
18
+ begin
19
+ path.choose
20
+ rescue SystemExit
21
+ end
22
+ }.to output(/There is no/).to_stdout
23
+ end
24
+
25
+ context "needs user input" do
26
+ let(:path) { Path.new(File.expand_path(File.dirname(__FILE__)) + '/support') }
27
+ before do
28
+ allow(STDIN).to receive(:gets) { "0" }
29
+ end
30
+
31
+ it "prints message when given path is a directory" do
32
+ expect {
33
+ path.choose
34
+ }.to output(/Select a path to the target schema/).to_stdout
35
+ end
36
+ end
37
+
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Schema do
3
+
4
+ let(:schema_data) { File.read(File.expand_path(File.dirname(__FILE__)) + '/support/schema.rb') }
5
+ let(:schema) { Schema.new(schema_data) }
6
+
7
+ describe ".parse" do
8
+ it "parses given schema file" do
9
+ expect(Schema.parse(schema_data)).to be_kind_of(Array)
10
+ end
11
+ end
12
+
13
+ describe "#table_names" do
14
+ it "returns table names" do
15
+ expect(schema.table_names).to eq(["users"])
16
+ end
17
+ end
18
+
19
+ describe "#to_script" do
20
+ it "is broken" do
21
+ pending
22
+ expect(schema.to_script).to eq("users")
23
+ end
24
+ end
25
+
26
+ describe "#table" do
27
+ it "returns parsed table by given table name as symbol" do
28
+ expect(schema.table(:users)).to be_kind_of(SchemaToScaffold::Table)
29
+ end
30
+
31
+ it "returns parsed table by given table index" do
32
+ expect(schema.table(0)).to be_kind_of(SchemaToScaffold::Table)
33
+ end
34
+
35
+ it "returns nil" do
36
+ expect(schema.table([])).to be_nil
37
+ end
38
+
39
+ it "returns nil" do
40
+ pending
41
+ expect(schema.table("")).to be_nil
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ RSpec.describe SchemaToScaffold do
2
+ it "has a version number" do
3
+ expect(SchemaToScaffold::VERSION).not_to be nil
4
+ end
5
+ end
@@ -0,0 +1,110 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+
7
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
8
+
9
+ require "simplecov"
10
+ SimpleCov.start 'test_frameworks'
11
+
12
+ require "schema_to_scaffold"
13
+
14
+ # Given that it is always loaded, you are encouraged to keep this file as
15
+ # light-weight as possible. Requiring heavyweight dependencies from this file
16
+ # will add to the boot time of your test suite on EVERY test run, even for an
17
+ # individual file that may not need all of that loaded. Instead, consider making
18
+ # a separate helper file that requires the additional dependencies and performs
19
+ # the additional setup, and require it from the spec files that actually need
20
+ # it.
21
+ #
22
+ # The `.rspec` file also contains a few flags that are not defaults but that
23
+ # users commonly want.
24
+ #
25
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
26
+ RSpec.configure do |config|
27
+ # rspec-expectations config goes here. You can use an alternate
28
+ # assertion/expectation library such as wrong or the stdlib/minitest
29
+ # assertions if you prefer.
30
+ config.expect_with :rspec do |expectations|
31
+ # This option will default to `true` in RSpec 4. It makes the `description`
32
+ # and `failure_message` of custom matchers include text for helper methods
33
+ # defined using `chain`, e.g.:
34
+ # be_bigger_than(2).and_smaller_than(4).description
35
+ # # => "be bigger than 2 and smaller than 4"
36
+ # ...rather than:
37
+ # # => "be bigger than 2"
38
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
39
+ end
40
+
41
+ # rspec-mocks config goes here. You can use an alternate test double
42
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
43
+ config.mock_with :rspec do |mocks|
44
+ # Prevents you from mocking or stubbing a method that does not exist on
45
+ # a real object. This is generally recommended, and will default to
46
+ # `true` in RSpec 4.
47
+ mocks.verify_partial_doubles = true
48
+ end
49
+
50
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
51
+ # have no way to turn it off -- the option exists only for backwards
52
+ # compatibility in RSpec 3). It causes shared context metadata to be
53
+ # inherited by the metadata hash of host groups and examples, rather than
54
+ # triggering implicit auto-inclusion in groups with matching metadata.
55
+ config.shared_context_metadata_behavior = :apply_to_host_groups
56
+
57
+ # The settings below are suggested to provide a good initial experience
58
+ # with RSpec, but feel free to customize to your heart's content.
59
+
60
+ # This allows you to limit a spec run to individual examples or groups
61
+ # you care about by tagging them with `:focus` metadata. When nothing
62
+ # is tagged with `:focus`, all examples get run. RSpec also provides
63
+ # aliases for `it`, `describe`, and `context` that include `:focus`
64
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
65
+ config.filter_run_when_matching :focus
66
+
67
+ # Allows RSpec to persist some state between runs in order to support
68
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
69
+ # you configure your source control system to ignore this file.
70
+ config.example_status_persistence_file_path = "spec/examples.txt"
71
+
72
+ # Limits the available syntax to the non-monkey patched syntax that is
73
+ # recommended. For more details, see:
74
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
75
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
76
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
77
+ config.disable_monkey_patching!
78
+
79
+ # This setting enables warnings. It's recommended, but in some cases may
80
+ # be too noisy due to issues in dependencies.
81
+ config.warnings = true
82
+
83
+ # Many RSpec users commonly either run the entire suite or an individual
84
+ # file, and it's useful to allow more verbose output when running an
85
+ # individual spec file.
86
+ if config.files_to_run.one?
87
+ # Use the documentation formatter for detailed output,
88
+ # unless a formatter has already been configured
89
+ # (e.g. via a command-line flag).
90
+ config.default_formatter = 'doc'
91
+ end
92
+
93
+ # Print the 10 slowest examples and example groups at the
94
+ # end of the spec run, to help surface which specs are running
95
+ # particularly slow.
96
+ config.profile_examples = 10
97
+
98
+ # Run specs in random order to surface order dependencies. If you find an
99
+ # order dependency and want to debug it, you can fix the order by providing
100
+ # the seed, which is printed after each run.
101
+ # --seed 1234
102
+ config.order = :random
103
+
104
+ # Seed global randomization in this process using the `--seed` CLI option.
105
+ # Setting this allows you to use `--seed` to deterministically reproduce
106
+ # test failures related to randomization by passing the same `--seed` value
107
+ # as the one that triggered the failure.
108
+ Kernel.srand config.seed
109
+
110
+ end
@@ -0,0 +1,7 @@
1
+ # random schema https://github.com/RailsApps/rails-devise-roles/blob/master/db/schema.rb
2
+ ActiveRecord::Schema.define(version: 20140828012915) do
3
+
4
+ create_table :users, force: :cascade do |t|
5
+ end
6
+
7
+ end
@@ -0,0 +1,24 @@
1
+ # random schema https://github.com/RailsApps/rails-devise-roles/blob/master/db/schema.rb
2
+ ActiveRecord::Schema.define(version: 20140828012915) do
3
+
4
+ create_table "users", force: :cascade do |t|
5
+ t.string "email", default: "", null: false
6
+ t.string "encrypted_password", default: "", null: false
7
+ t.string "reset_password_token"
8
+ t.datetime "reset_password_sent_at"
9
+ t.datetime "remember_created_at"
10
+ t.integer "sign_in_count", default: 0, null: false
11
+ t.datetime "current_sign_in_at"
12
+ t.datetime "last_sign_in_at"
13
+ t.string "current_sign_in_ip"
14
+ t.string "last_sign_in_ip"
15
+ t.datetime "created_at"
16
+ t.datetime "updated_at"
17
+ t.string "name"
18
+ t.integer "role"
19
+ end
20
+
21
+ add_index "users", ["email"], name: "index_users_on_email", unique: true
22
+ add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
23
+
24
+ end
@@ -0,0 +1,17 @@
1
+ create_table "users", force: :cascade do |t|
2
+ t.string "email", default: "", null: false
3
+ t.string "encrypted_password", default: "", null: false
4
+ t.string "reset_password_token"
5
+ t.datetime "reset_password_sent_at"
6
+ t.datetime "remember_created_at"
7
+ t.integer "sign_in_count", default: 0, null: false
8
+ t.datetime "current_sign_in_at"
9
+ t.datetime "last_sign_in_at"
10
+ t.string "current_sign_in_ip"
11
+ t.string "last_sign_in_ip"
12
+ t.datetime "created_at"
13
+ t.datetime "updated_at"
14
+ t.string "name"
15
+ t.integer "role"
16
+ t.index ["basket_id"], name: "index_orders_on_basket_id", using: :btree
17
+ end
@@ -0,0 +1,33 @@
1
+ module SchemaToScaffold
2
+ RSpec.describe Table do
3
+
4
+ let(:table_data) { File.read(File.expand_path(File.dirname(__FILE__)) + '/support/table.rb') }
5
+ let(:email_attribute) { Attribute.new("email", "string") }
6
+ let(:password_attribute) { Attribute.new("password", "string") }
7
+
8
+ describe "#to_script" do
9
+ let(:table) { Table.new("users", [email_attribute, password_attribute]) }
10
+
11
+ it "returns rails generate line" do
12
+ expect(table.to_script("model", true)).to eq(["rails generate model User email:string password:string", "\n\n"])
13
+ end
14
+
15
+ it "returns rails generate line without migrations" do
16
+ expect(table.to_script("model", false)).to eq(["rails generate model User email:string password:string", " --no-migration", "\n\n"])
17
+ end
18
+
19
+ it "raises an exception" do
20
+ allow(email_attribute).to receive(:to_script).and_raise(StandardError, "something went wrong")
21
+ expect { table.to_script("model", true) }.to output(/something went wrong/).to_stdout
22
+ end
23
+ end
24
+
25
+ describe ".parse" do
26
+ let(:table) { Table.parse(table_data) }
27
+ it "parses given table data" do
28
+ expect(table).to be_kind_of(SchemaToScaffold::Table)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_to_scaffold
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Soares
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-25 00:00:00.000000000 Z
12
+ date: 2016-12-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -45,8 +45,36 @@ dependencies:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: 0.10.0
48
- description: |2
49
- Command line app which parses a schema.rb file obtained from your rails repo or by running rake:schema:dump
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ type: :development
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ description: " Command line app which parses a schema.rb file obtained from your
77
+ rails repo or by running rake:schema:dump\n"
50
78
  email:
51
79
  - jsoaresgeral@gmail.com
52
80
  - hlsp999@gmail.com
@@ -56,6 +84,8 @@ extensions: []
56
84
  extra_rdoc_files: []
57
85
  files:
58
86
  - ".gitignore"
87
+ - ".rspec"
88
+ - ".ruby-gemset"
59
89
  - ".ruby-version"
60
90
  - CHANGELOG.md
61
91
  - Gemfile
@@ -65,16 +95,31 @@ files:
65
95
  - bin/scaffold
66
96
  - lib/schema_to_scaffold.rb
67
97
  - lib/schema_to_scaffold/attribute.rb
98
+ - lib/schema_to_scaffold/cli.rb
99
+ - lib/schema_to_scaffold/clipboard.rb
100
+ - lib/schema_to_scaffold/help.rb
68
101
  - lib/schema_to_scaffold/path.rb
69
102
  - lib/schema_to_scaffold/schema.rb
70
103
  - lib/schema_to_scaffold/table.rb
71
104
  - lib/schema_to_scaffold/version.rb
72
105
  - schema_to_scaffold.gemspec
106
+ - spec/attribute_spec.rb
107
+ - spec/cli_spec.rb
108
+ - spec/clipboard_spec.rb
109
+ - spec/help_spec.rb
110
+ - spec/path_spec.rb
111
+ - spec/schema_spec.rb
112
+ - spec/schema_to_scaffold_spec.rb
113
+ - spec/spec_helper.rb
114
+ - spec/support/empty_schema.rb
115
+ - spec/support/schema.rb
116
+ - spec/support/table.rb
117
+ - spec/table_spec.rb
73
118
  homepage: http://github.com/frenesim/schema_to_scaffold
74
119
  licenses:
75
120
  - MIT
76
121
  metadata: {}
77
- post_install_message: Thanks for installing!
122
+ post_install_message:
78
123
  rdoc_options: []
79
124
  require_paths:
80
125
  - lib
@@ -90,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
135
  version: '0'
91
136
  requirements: []
92
137
  rubyforge_project:
93
- rubygems_version: 2.2.2
138
+ rubygems_version: 2.5.1
94
139
  signing_key:
95
140
  specification_version: 4
96
141
  summary: Generate rails scaffold script from a schema.rb file.