schema_doc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2df6422967bcdbc49244c380b605a7b76abd6b6
4
+ data.tar.gz: aba70068fb2ae205f52eff346ef19744c168642b
5
+ SHA512:
6
+ metadata.gz: 47fc05bed1551769753e6ef047e11ff28f3b4d978fc296aebc8c086f9cb420c1b37c215b53f0124d3aeba2853a776179c30eb05df92a16e1c1976ca424c22043
7
+ data.tar.gz: dee20aafd3d1ce069fdbdc1ef97d4863216dbdfab36b768cc40ea3b34d26c89774480526f3d1ce7be12dbad8640858696545fea7e58c38834f87aaffb6612b97
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
+ vendor/bundle
19
+ spec/db/*.sqlite3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in schema_doc.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 mizokami
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,47 @@
1
+ # SchemaDoc
2
+
3
+ SchemaDoc outputs database schema for your Rails applications in markdown style
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'schema_doc', :github => 'mizoR/schema_doc'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ ```
18
+ $ rake schema_doc:out
19
+ ```
20
+
21
+ When your Rails application has following models:
22
+
23
+ ```ruby
24
+
25
+ class User < ActiveRecord::Base
26
+ has_one :blog, :foreign_key => :owner_id
27
+ end
28
+
29
+ class Blog < ActiveRecord::Base
30
+ has_many :entries
31
+ belongs_to :owner, :class_name => 'User'
32
+ end
33
+
34
+ class Entry < ActiveRecord::Base
35
+ belongs_to :blog
36
+ end
37
+ ```
38
+
39
+ SchemaDoc will output [THIS](https://github.com/mizoR/schema_doc/tree/master/example/sample_output.md).
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,30 @@
1
+
2
+ # blogs
3
+
4
+ ## Blog
5
+
6
+ | name | human name | sql type | primary | default | limit |
7
+ | ---- | ---------- | -------- | ------- | ------- | ----- |
8
+ | id | Id | INTEGER | true | | |
9
+ | name | Name | varchar(255) | false | | 255 |
10
+ | owner_id | Owner | integer | false | | |
11
+
12
+ # entries
13
+
14
+ ## Entry
15
+
16
+ | name | human name | sql type | primary | default | limit |
17
+ | ---- | ---------- | -------- | ------- | ------- | ----- |
18
+ | id | Id | INTEGER | true | | |
19
+ | title | Title | varchar(255) | false | | 255 |
20
+ | body | Body | varchar(255) | false | | 255 |
21
+
22
+ # users
23
+
24
+ ## User
25
+
26
+ | name | human name | sql type | primary | default | limit |
27
+ | ---- | ---------- | -------- | ------- | ------- | ----- |
28
+ | id | Id | INTEGER | true | | |
29
+ | nick | Nick | varchar(255) | false | | 255 |
30
+
@@ -0,0 +1,47 @@
1
+ require 'erb'
2
+
3
+ module SchemaDoc
4
+ class Document
5
+ class << self
6
+ def read
7
+ document = []
8
+
9
+ descendants_group_by_table_name.sort.each do |table_name, descendants|
10
+ document << template(table_name, descendants)
11
+ end
12
+
13
+ document.join
14
+ end
15
+
16
+ private
17
+
18
+ def template(table_name, descendant)
19
+ case descendant
20
+ when Array
21
+ "# #{table_name}\n" + descendant.map {|d| template(table_name, d)}.join
22
+ else
23
+ ERB.new(<<MARKDOWN).result(binding)
24
+ <%# coding: UTF-8 %>
25
+ ## <%= descendant %>
26
+
27
+ | name | human name | sql type | primary | default | limit |
28
+ | ---- | ---------- | -------- | ------- | ------- | ----- |
29
+ <% descendant.columns.each do |column| %>| <%= column.name %> | <%= descendant.human_attribute_name(column.name) %> | <%= column.sql_type %> | <%= column.primary %> | <%= column.default %> | <%= column.limit %> |
30
+ <% end %>
31
+ MARKDOWN
32
+ end
33
+ end
34
+
35
+ def descendants
36
+ ActiveRecord::Base.descendants
37
+ end
38
+
39
+ def descendants_group_by_table_name
40
+ descendants.group_by {|descendant|
41
+ descendant.table_name
42
+ }
43
+ end
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,9 @@
1
+ # coding: utf-8
2
+ module SchemaDoc
3
+ class Railtie < ::Rails::Railtie
4
+ rake_tasks do
5
+ load 'schema_doc/tasks.rake'
6
+ end
7
+ end
8
+ end
9
+
@@ -0,0 +1,9 @@
1
+ namespace :schema_doc do
2
+ task :out => :environment do
3
+ require 'schema_doc'
4
+ Dir[Rails.root + 'app/models/**/*.rb'].each do |path|
5
+ require path
6
+ end
7
+ puts SchemaDoc::Document.read if !Rake.application.options.silent
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module SchemaDoc
2
+ VERSION = "0.0.1"
3
+ end
data/lib/schema_doc.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "schema_doc/version"
2
+ require "schema_doc/document"
3
+ require "schema_doc/railtie" if defined?(Rails)
4
+
5
+ module SchemaDoc
6
+ def self.root
7
+ File.expand_path('../..', __FILE__)
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'schema_doc/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "schema_doc"
8
+ spec.version = SchemaDoc::VERSION
9
+ spec.authors = ["mizokami"]
10
+ spec.email = ["suzunatsu@yahoo.com"]
11
+ spec.description = %q{Output database schema in markdown style.}
12
+ spec.summary = %q{Output database schema in markdown style.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake", ">= 3.2.16"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "activerecord"
25
+ spec.add_development_dependency "sqlite3"
26
+ end
data/spec/db/.gitkeep ADDED
@@ -0,0 +1 @@
1
+
data/spec/helper.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'schema_doc'
2
+ require 'active_record'
3
+
4
+ dbfile = File.join(SchemaDoc.root, 'spec/db/test.sqlite3')
5
+
6
+ if File.exist?(dbfile)
7
+ File.delete(dbfile)
8
+ end
9
+
10
+ ActiveRecord::Base.establish_connection(
11
+ 'adapter' => 'sqlite3',
12
+ 'database' => dbfile
13
+ )
14
+
15
+ class CreateUsers < ActiveRecord::Migration
16
+ def self.up
17
+ create_table :users do |t|
18
+ t.string :nick
19
+ end
20
+ end
21
+
22
+ def self.down
23
+ drop_table :users
24
+ end
25
+ end
26
+
27
+ class CreateBlogs < ActiveRecord::Migration
28
+ def self.up
29
+ create_table :blogs do |t|
30
+ t.string :name
31
+ t.integer :owner_id
32
+ end
33
+ end
34
+
35
+ def self.down
36
+ drop_table :blogs
37
+ end
38
+ end
39
+
40
+ class CreateEntries < ActiveRecord::Migration
41
+ def self.up
42
+ create_table :entries do|t|
43
+ t.string :title
44
+ t.string :body
45
+ end
46
+ end
47
+
48
+ def self.down
49
+ drop_table :entries
50
+ end
51
+ end
52
+
53
+ [CreateUsers, CreateBlogs, CreateEntries].map(&:new).each(&:up)
54
+
55
+ class User < ActiveRecord::Base
56
+ has_one :blog, :foreign_key => :owner_id
57
+ end
58
+
59
+ class Blog < ActiveRecord::Base
60
+ has_many :entries
61
+ belongs_to :owner, :class_name => 'User'
62
+ end
63
+
64
+ class Entry < ActiveRecord::Base
65
+ belongs_to :blog
66
+ end
67
+
@@ -0,0 +1,44 @@
1
+ require 'helper'
2
+
3
+ describe SchemaDoc::Document do
4
+ it do
5
+ puts SchemaDoc::Document.read
6
+ end
7
+ describe 'send(:template)' do
8
+ let :template do
9
+ SchemaDoc::Document.send(:template, *args)
10
+ end
11
+
12
+ context 'When table is "users"' do
13
+ let :args do
14
+ ['users', [User]]
15
+ end
16
+
17
+ it 'matches table name' do
18
+ expect(template).to match(/^# #{args[0]}$/)
19
+ end
20
+
21
+ it 'matches model name' do
22
+ args[1].each do |model|
23
+ expect(template).to match(/^## #{model.to_s}$/)
24
+ end
25
+ end
26
+
27
+ it 'matches header ' do
28
+ expect(template).to match(/^| name | human name | sql type | primary | default | limit |$/)
29
+ end
30
+
31
+ it 'matches separator ' do
32
+ expect(template).to match(/^| ---- | ---------- | -------- | ------- | ------- | ----- |$/)
33
+ end
34
+
35
+ it 'matches some columns ' do
36
+ args[1].each do |model|
37
+ model.columns.each do |column|
38
+ expect(template).to match(/^| #{column.name} | /)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1 @@
1
+ require 'helper'
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schema_doc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mizokami
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.16
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.16
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Output database schema in markdown style.
84
+ email:
85
+ - suzunatsu@yahoo.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - example/sample_output.md
96
+ - lib/schema_doc.rb
97
+ - lib/schema_doc/document.rb
98
+ - lib/schema_doc/railtie.rb
99
+ - lib/schema_doc/tasks.rake
100
+ - lib/schema_doc/version.rb
101
+ - schema_doc.gemspec
102
+ - spec/db/.gitkeep
103
+ - spec/helper.rb
104
+ - spec/schema_doc/document_spec.rb
105
+ - spec/schema_doc_spec.rb
106
+ homepage: ''
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.0.3
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Output database schema in markdown style.
130
+ test_files:
131
+ - spec/db/.gitkeep
132
+ - spec/helper.rb
133
+ - spec/schema_doc/document_spec.rb
134
+ - spec/schema_doc_spec.rb