schema_to_scaffold 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .project
19
+ /schema.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in schema_to_scaffold.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Humberto Pinto, João Soares
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ Schema to Scaffold
2
+ ==================
3
+
4
+ After I cross with rake db:schema:dump I feel that it could do a litle more in my case. So there it is.
5
+
6
+ Usage
7
+ -------
8
+
9
+ The code is very simple and the usage is not friendly but almost.
10
+
11
+ 1. download the zip (or copy paste de code)
12
+ 2. extract to folder
13
+ 3. cd folder
14
+ 4. if your projects are outside your user folder: edit the main.rb and change the path to point to your schema.rb (generated with the rake db:schema:dump)
15
+ 5. ruby main.rb
16
+ 6. chose the path to your schema.rb
17
+ 7. chose your table
18
+ 8. copy the rails generate scaffold command
19
+
20
+ * Eight quick steps that will save you a lot of time
21
+ * It will give some alerts if your names are not following the rails convention.
22
+
23
+ read a litle about [rails convention](http://itsignals.cascadia.com.au/?p=7)
24
+
25
+ Contributing
26
+ ------------
27
+
28
+ Want to contribute? Great!
29
+
30
+ 1. Fork it.
31
+ 2. Create a branch (`git checkout -b my_schema_to_scafold`)
32
+ 3. Commit your changes (`git commit -am "Added great stuff"`)
33
+ 4. Push to the branch (`git push origin my_schema_to_scafold`)
34
+ 5. Open a [Pull Request][1]
35
+ 6. That's all!!
36
+
37
+ [1]: http://github.com/frenesim/schema_to_scaffold/pulls
38
+
39
+ Colaborate
40
+ ------------
41
+ if you want to colaborate send me an email please.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/scaffold ADDED
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift File.expand_path('../lib', File.dirname(__FILE__))
3
+ require 'schema_to_scaffold'
4
+
5
+ HELP =<<-END_OF_HELP
6
+ Usage: scaffold [OPTION] PATH
7
+ Generate a rails scaffold script for the given schema.rb file
8
+ PATH is the path to the schema.rb file.
9
+
10
+ OPTIONS:
11
+ -x put the rails scaffold script in your clipboard using xclip, type:
12
+ sudo apt-get install xclip
13
+ if you don't have it
14
+
15
+ END_OF_HELP
16
+
17
+ xclip = ARGV.delete('-x')
18
+ if ARGV[0].nil? or ARGV.delete('-h')
19
+ puts HELP
20
+ else
21
+ data = File.open(ARGV[0], 'r') {|f| f.read }
22
+ until ARGV.empty? do
23
+ ARGV.shift
24
+ end
25
+
26
+ schema = SchemaToScaffold::Schema.new(data)
27
+
28
+ begin
29
+ schema.check_rails_naming_conventions
30
+ rescue NotFollowingConventionsError => msg
31
+ puts msg
32
+ end
33
+
34
+ puts "\nLoaded tables:"
35
+ schema.table_names.each_with_index {|name,i| puts "#{i}. #{name}" }
36
+
37
+ begin
38
+ print "\nSelect a table: "
39
+ end while schema.table_names[(table_id = gets.to_i)].nil?
40
+
41
+ script = SchemaToScaffold.generate_script(schema, table_id)
42
+ puts "\n#{script}"
43
+ if xclip
44
+ puts("\n(copied to your clipboard)")
45
+ exec("echo '#{script}' | xclip -selection c")
46
+ end
47
+ end
@@ -0,0 +1,15 @@
1
+ require 'active_support/inflector'
2
+ require 'schema_to_scaffold/version'
3
+ require 'schema_to_scaffold/schema'
4
+ require 'schema_to_scaffold/table'
5
+ require 'schema_to_scaffold/attribute'
6
+
7
+ module SchemaToScaffold
8
+ extend self
9
+
10
+ def self.generate_script(schema, table=nil)
11
+ schema = Schema.new(schema) unless schema.is_a? Schema
12
+ return schema.to_script if table.nil?
13
+ schema.table(table).to_script
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module SchemaToScaffold
2
+ class Attribute
3
+
4
+ attr_reader :name, :type
5
+
6
+ def initialize(type, name)
7
+ @name, @type = name, type
8
+ end
9
+
10
+ def to_script
11
+ "#{name}:#{type}"
12
+ end
13
+
14
+ def self.parse(attribute)
15
+ Attribute.new(*attribute.match(/t\.(\w+)\s+"(\w+)"/).captures)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,37 @@
1
+ module SchemaToScaffold
2
+ class Schema
3
+
4
+ attr_reader :data, :tables
5
+
6
+ ERROR_MSG = "Warning: You should change the table names according to rails conventions."
7
+
8
+ def initialize(data)
9
+ @data, @tables = data, Schema.parse(data)
10
+ end
11
+
12
+ def table_names
13
+ tables.map(&:name)
14
+ end
15
+
16
+ def check_rails_naming_conventions
17
+ raise NotFollowingConventionsError, ERROR_MSG if table_names.any? {|n| n == n.singularize or n == n.camelcase }
18
+ end
19
+
20
+ def table(id)
21
+ case id
22
+ when Symbol then table(id.to_s)
23
+ when String then tables[table_names.index(id)]
24
+ when Fixnum then tables[id]
25
+ else nil
26
+ end
27
+ end
28
+
29
+ def to_script
30
+ tables.map(&:to_script)
31
+ end
32
+
33
+ def self.parse(data)
34
+ data.split(/create_/)[1..-1].map {|table_data| Table.parse table_data }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,20 @@
1
+ module SchemaToScaffold
2
+ class Table
3
+
4
+ attr_reader :attributes, :name
5
+
6
+ def initialize(name, attributes)
7
+ @name, @attributes = name, attributes
8
+ end
9
+
10
+ def to_script
11
+ "rails g scaffold #{name} #{attributes.map(&:to_script).join(' ')}"
12
+ end
13
+
14
+ def self.parse(table_data)
15
+ name = table_data.match(/table "(.+)"/).captures.first;
16
+ atts = table_data.lines.to_a.select {|line| line =~ /t\.\w+/ }.map {|att| Attribute.parse att }
17
+ Table.new(name, atts)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module SchemaToScaffold
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'schema_to_scaffold/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "schema_to_scaffold"
8
+ gem.version = SchemaToScaffold::VERSION
9
+ gem.authors = ["Humberto Pinto", "João Soares"]
10
+ gem.email = ["h.lsp999@gmail.com", "jsoaresgeral@gmail.com"]
11
+ gem.description = <<-EOD
12
+ Command line app for windows and linux which parses a schema.rb file obtained from your rails repo or by running rake:schema:dump
13
+ EOD
14
+ gem.summary = %q{Generate rails scaffold script from a schema.rb file.}
15
+ gem.homepage = "http://github.com/frenesim/schema_to_scaffold"
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_runtime_dependency "active_support"
22
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schema_to_scaffold
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Humberto Pinto
9
+ - João Soares
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-11-26 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: active_support
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ description: ! ' Command line app for windows and linux which parses a schema.rb
32
+ file obtained from your rails repo or by running rake:schema:dump
33
+
34
+ '
35
+ email:
36
+ - h.lsp999@gmail.com
37
+ - jsoaresgeral@gmail.com
38
+ executables:
39
+ - scaffold
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE.txt
46
+ - README.md
47
+ - Rakefile
48
+ - bin/scaffold
49
+ - lib/schema_to_scaffold.rb
50
+ - lib/schema_to_scaffold/attribute.rb
51
+ - lib/schema_to_scaffold/schema.rb
52
+ - lib/schema_to_scaffold/table.rb
53
+ - lib/schema_to_scaffold/version.rb
54
+ - schema_to_scaffold.gemspec
55
+ homepage: http://github.com/frenesim/schema_to_scaffold
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.24
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: Generate rails scaffold script from a schema.rb file.
79
+ test_files: []