Class: SchemaToScaffold::CLI
- Inherits:
-
Object
- Object
- SchemaToScaffold::CLI
- Defined in:
- lib/schema_to_scaffold/cli.rb
Constant Summary collapse
- 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"
Class Method Summary collapse
-
.generate_script(schema, table = nil, target, migration_flag) ⇒ Object
Generates the rails scaffold script.
-
.parse_arguments(argv) ⇒ Object
Parses ARGV and returns a hash of options.
- .start(*args) ⇒ Object
Class Method Details
.generate_script(schema, table = nil, target, migration_flag) ⇒ Object
Generates the rails scaffold script
100 101 102 103 104 |
# File 'lib/schema_to_scaffold/cli.rb', line 100 def self.generate_script(schema, table=nil, target, migration_flag) schema = Schema.new(schema) unless schema.is_a?(Schema) return schema.to_script if table.nil? schema.table(table).to_script(target, migration_flag) end |
.parse_arguments(argv) ⇒ Object
Parses ARGV and returns a hash of options.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/schema_to_scaffold/cli.rb', line 75 def self.parse_arguments(argv) if argv_index = argv.index("-p") path = argv.delete_at(argv_index + 1) argv.delete('-p') end args = { clipboard: argv.delete('-c'), # check for clipboard flag factory_bot: argv.delete('-f'), # factory_bot instead of scaffold migration: argv.delete('-m'), # generate migrations help: argv.delete('-h'), # check for help flag path: path # get path to file(s) } if argv.empty? args else puts "\n------\nWrong set of arguments.\n------\n" puts Help. exit end end |
.start(*args) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/schema_to_scaffold/cli.rb', line 7 def self.start(*args) ## Argument conditions opts = parse_arguments(args) if opts[:help] puts Help. exit 0 end ## looking for /schema\S*.rb$/ in user directory paths = Path.new(opts[:path]) path = paths.choose unless opts[:path].to_s.match(/\.rb$/) ## Opening file path ||= opts[:path] begin data = File.open(path, 'r') { |f| f.read } rescue puts "\nUnable to open file '#{path}'" exit 1 rescue Interrupt => e exit 1 end ## Generate scripts from schema schema = Schema.new(data) begin raise if schema.table_names.empty? puts "\nLoaded tables:" schema.print_table_names puts TABLE_OPTIONS print "\nSelect a table: " rescue puts "Could not find tables in '#{path}'" exit 1 end input = STDIN.gets.strip begin tables = schema.select_tables(input) raise if tables.empty? rescue puts "Not a valid input. #{TABLE_OPTIONS}" exit 1 rescue Interrupt => e exit 1 end script = [] target = opts[:factory_bot] ? "factory_bot:model" : "scaffold" migration_flag = opts[:migration] ? true : false tables.each do |table_id| script << generate_script(schema, table_id, target, migration_flag) end output = script.join("") puts "\nScript for #{target}:\n\n" puts output if opts[:clipboard] puts("\n(copied to your clipboard)") Clipboard.new(output).command end end |