schema_parser 0.1.2

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ec29e11d329c979f628880bbff69f1fc8035c5d
4
+ data.tar.gz: 0a50d11241e45b4b11bd9a5a2633359cc6fc0883
5
+ SHA512:
6
+ metadata.gz: ae5454e9a565cf7e01d5d6b2f86fc8c84807ffa8fdb6990847dc1d2462c5fbd886754ecd7d6db07f4cf027ead87bd606faf29564e9ee51508942f612f2d20476
7
+ data.tar.gz: bfe7cfeed0a3c745d80f5fbeafa9056072995ea9c38bc7472d808864a5dcf065717537e3d1817a7292952a0ed5a2cbefb6c0bdc9ad3fc7c8dccd03ca375ca8b4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at banana@eurus.cn. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in schema_parser.gemspec
4
+ gemspec
5
+ gem 'rake'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 banana
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,142 @@
1
+ # SchemaParser
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/schema_parser`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'schema_parser'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install schema_parser
22
+
23
+ ## Requirements
24
+
25
+ You should have [Graphviz](http://www.graphviz.org/) installed on your machine.
26
+
27
+ ## Example
28
+
29
+ Consider you have a file called `example.schema.txt`
30
+
31
+ ```txt
32
+ Club
33
+ ==================================================
34
+ id int(11)
35
+ name varchar(255)
36
+ address varchar(255)
37
+ expired_at datetime
38
+ created_at datetime
39
+ updated_at datetime
40
+ ecard int(11)
41
+ image_keys varchar(255)
42
+ portable tinyint(1)
43
+ --------------------------------------------------
44
+
45
+ School
46
+ ==================================================
47
+ id int(11)
48
+ name varchar(255)
49
+ address varchar(255)
50
+ ecard int(11)
51
+ expired_at datetime
52
+ created_at datetime
53
+ updated_at datetime
54
+ portable tinyint(1)
55
+ --------------------------------------------------
56
+
57
+ Game
58
+ ==================================================
59
+ id int(11)
60
+ start_at datetime
61
+ end_at datetime
62
+ lat double
63
+ lng double
64
+ address varchar(255)
65
+ created_at datetime
66
+ updated_at datetime
67
+ name varchar(255)
68
+ image_key varchar(255)
69
+ image_keys varchar(255)
70
+ --------------------------------------------------
71
+
72
+ ClubGame
73
+ ==================================================
74
+ game_id int(11)
75
+ club_id int(11)
76
+ --------------------------------------------------
77
+
78
+ SchoolGame
79
+ ==================================================
80
+ game_id int(11)
81
+ school_id int(11)
82
+ --------------------------------------------------
83
+
84
+ ClubProfile
85
+ ==================================================
86
+ id int(11)
87
+ user_id int(11)
88
+ club_id int(11)
89
+ avatar_key varchar(255)
90
+ course_id int(11)
91
+ gender int(11)
92
+ birthday datetime
93
+ point int(11)
94
+ ce int(11)
95
+ created_at datetime
96
+ updated_at datetime
97
+ realname varchar(255)
98
+ --------------------------------------------------
99
+
100
+ SchoolProfile
101
+ ==================================================
102
+ id int(11)
103
+ user_id int(11)
104
+ avatar_key varchar(255)
105
+ classroom_id int(11)
106
+ gender int(11)
107
+ birthday datetime
108
+ point int(11)
109
+ ce int(11)
110
+ created_at datetime
111
+ updated_at datetime
112
+ realname varchar(255)
113
+ school_id int(11)
114
+ --------------------------------------------------
115
+ ```
116
+
117
+ Then you can write a ruby file like below
118
+
119
+ ```ruby
120
+ sp = SchemaGenerator::Parser.new('./example.schema.txt', 'example.dot')
121
+ sp.draw
122
+ ```
123
+
124
+ You will get a dot file and a png file.
125
+
126
+ ![example.dot.png](https://github.com/KTFootball/schema_simple_er_map/blob/master/example.dot.png "example.dot.png")
127
+
128
+ ## Development
129
+
130
+ 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.
131
+
132
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
133
+
134
+ ## Contributing
135
+
136
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/schema_parser. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
137
+
138
+
139
+ ## License
140
+
141
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
142
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "schema_parser"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ digraph g {
2
+ node [shape=record,color=Red,fontname=Courier];
3
+ edge [color=Blue]
4
+ Club [label="Club|{name|address|expired_at|ecard|image_keys|portable|club_id}"];
5
+ School [label="School|{name|address|ecard|expired_at|portable|school_id}"];
6
+ Game [label="Game|{start_at|end_at|lat|lng|address|name|image_key|image_keys|game_id}"];
7
+ ClubGame [label="ClubGame|{game_id|club_id|club_game_id}"];
8
+ SchoolGame [label="SchoolGame|{game_id|school_id|school_game_id}"];
9
+ ClubProfile [label="ClubProfile|{user_id|club_id|avatar_key|course_id|gender|birthday|point|ce|realname|club_profile_id}"];
10
+ SchoolProfile [label="SchoolProfile|{user_id|avatar_key|classroom_id|gender|birthday|point|ce|realname|school_id|school_profile_id}"];
11
+ Club -> ClubGame [dir="none"]
12
+ Club -> ClubProfile [dir="none"]
13
+ School -> SchoolGame [dir="none"]
14
+ School -> SchoolProfile [dir="none"]
15
+ Game -> ClubGame [dir="none"]
16
+ Game -> SchoolGame [dir="none"]
17
+ }
@@ -0,0 +1,83 @@
1
+ Club
2
+ ==================================================
3
+ id int(11)
4
+ name varchar(255)
5
+ address varchar(255)
6
+ expired_at datetime
7
+ created_at datetime
8
+ updated_at datetime
9
+ ecard int(11)
10
+ image_keys varchar(255)
11
+ portable tinyint(1)
12
+ --------------------------------------------------
13
+
14
+ School
15
+ ==================================================
16
+ id int(11)
17
+ name varchar(255)
18
+ address varchar(255)
19
+ ecard int(11)
20
+ expired_at datetime
21
+ created_at datetime
22
+ updated_at datetime
23
+ portable tinyint(1)
24
+ --------------------------------------------------
25
+
26
+ Game
27
+ ==================================================
28
+ id int(11)
29
+ start_at datetime
30
+ end_at datetime
31
+ lat double
32
+ lng double
33
+ address varchar(255)
34
+ created_at datetime
35
+ updated_at datetime
36
+ name varchar(255)
37
+ image_key varchar(255)
38
+ image_keys varchar(255)
39
+ --------------------------------------------------
40
+
41
+ ClubGame
42
+ ==================================================
43
+ game_id int(11)
44
+ club_id int(11)
45
+ --------------------------------------------------
46
+
47
+ SchoolGame
48
+ ==================================================
49
+ game_id int(11)
50
+ school_id int(11)
51
+ --------------------------------------------------
52
+
53
+ ClubProfile
54
+ ==================================================
55
+ id int(11)
56
+ user_id int(11)
57
+ club_id int(11)
58
+ avatar_key varchar(255)
59
+ course_id int(11)
60
+ gender int(11)
61
+ birthday datetime
62
+ point int(11)
63
+ ce int(11)
64
+ created_at datetime
65
+ updated_at datetime
66
+ realname varchar(255)
67
+ --------------------------------------------------
68
+
69
+ SchoolProfile
70
+ ==================================================
71
+ id int(11)
72
+ user_id int(11)
73
+ avatar_key varchar(255)
74
+ classroom_id int(11)
75
+ gender int(11)
76
+ birthday datetime
77
+ point int(11)
78
+ ce int(11)
79
+ created_at datetime
80
+ updated_at datetime
81
+ realname varchar(255)
82
+ school_id int(11)
83
+ --------------------------------------------------
@@ -0,0 +1,73 @@
1
+ require 'schema_parser/version'
2
+ require 'schema_parser/schema_node'
3
+ require 'schema_parser/schema_link'
4
+
5
+ class String
6
+ def underscore
7
+ self.gsub(/::/, '/').
8
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
9
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
10
+ tr("-", "_").
11
+ downcase
12
+ end
13
+ end
14
+
15
+ module SchemaParser
16
+ class Parser
17
+ def initialize(source, target)
18
+ @source, @target = source, target
19
+ @nodes = []
20
+ @edges = {}
21
+ end
22
+
23
+ def draw
24
+ split_into_nodes
25
+ File.open(@target, 'w+') do |writeable|
26
+ writeable.write("digraph g {\n")
27
+ writeable.write("node [shape=record,color=Red,fontname=Courier];\n")
28
+ writeable.write("edge [color=Blue]\n")
29
+
30
+ @nodes.each do |node|
31
+ writeable.write node.to_struct
32
+ end
33
+
34
+ @nodes.combination(2).to_a.each do |node_a, node_b|
35
+ link = SchemaLink.new(node_a, node_b)
36
+ @edges["#{node_a.name}_#{node_b.name}"] = link.link?
37
+ end
38
+
39
+ @edges.select {|k,v|v == true}.each do |k,v|
40
+ writeable.write "#{k.split('_')[0]} -> #{k.split('_')[1]} [dir=\"none\"]\n"
41
+ end
42
+
43
+ writeable.write("}\n")
44
+ end
45
+ end
46
+
47
+ private
48
+ def split_into_nodes
49
+ begin
50
+ @nodes = File.open(@source, 'r') { |file| file.readlines }
51
+ .reject{ |line| line.match(/^$/) }
52
+ .join()
53
+ .split("--------------------------------------------------")
54
+ .map do |b|
55
+ sources = b.lines.reject{|line| line == "\n"}
56
+ name = sources.first.gsub("\n",'')
57
+ attrs = sources[2..-1].map { |e| e.split(' ')
58
+ .join(':') }
59
+ .reject{ |s| s.include? "created_at" }
60
+ .reject{ |s| s.include? "updated_at"}
61
+ .reject{ |s| s == "id:int(11)"}
62
+ .concat ["#{name.underscore}_id:int(11)"]
63
+ .uniq
64
+ SchemaNode.new(name, attrs)
65
+ end
66
+ rescue Exception => e
67
+ puts 'Parse Error!'
68
+ @nodes = []
69
+ end
70
+ end
71
+
72
+ end
73
+ end
@@ -0,0 +1,14 @@
1
+ module SchemaParser
2
+ class SchemaLink
3
+ def initialize(node_a, node_b)
4
+ @node_a = node_a
5
+ @node_b = node_b
6
+ end
7
+
8
+ def link?
9
+ case_a = @node_b.attrs.include?("#{@node_a.name.underscore}_id:int(11)")
10
+ case_b = @node_a.attrs.include?("#{@node_b.name.underscore}_id:int(11)")
11
+ case_a || case_b
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ module SchemaParser
2
+ class SchemaNode
3
+ attr_accessor :name, :attrs
4
+ def initialize(name, attrs)
5
+ @name = name
6
+ @attrs = attrs
7
+ end
8
+
9
+ def to_struct
10
+ "#{@name} [label=\"#{@name}|{#{@attrs.map {|e| e.split(':')[0]}.join('|')}}\"];\n"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module SchemaParser
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'schema_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "schema_parser"
8
+ spec.version = SchemaParser::VERSION
9
+ spec.authors = ["banana"]
10
+ spec.email = ["banana@eurus.cn"]
11
+
12
+ spec.summary = %q{This is a simple schema parser}
13
+ spec.description = %q{This is a simple schema parser to convert specifed schema.txt to dot file}
14
+ spec.homepage = "https://github.com/lostpupil/schema_parser"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.14"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: schema_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - banana
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-19 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: This is a simple schema parser to convert specifed schema.txt to dot
42
+ file
43
+ email:
44
+ - banana@eurus.cn
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - example.dot
58
+ - example.schema.txt
59
+ - lib/schema_parser.rb
60
+ - lib/schema_parser/schema_link.rb
61
+ - lib/schema_parser/schema_node.rb
62
+ - lib/schema_parser/version.rb
63
+ - schema_parser.gemspec
64
+ homepage: https://github.com/lostpupil/schema_parser
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.6.11
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: This is a simple schema parser
88
+ test_files: []