zan_tools 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b433c97caefb8d9cb0cf35ff8f32ada3677d6cdab08bbc26eaaf0135f3a04229
4
+ data.tar.gz: 937dc277b1d5f842d316f6ccbd648d3c9b5c8e5d41db0f82fd419ffb0dcadc99
5
+ SHA512:
6
+ metadata.gz: 6e5d2ac5cde35a010c4d9d687d028892b6f5f6e39c054a7cdf94521d902d76617b526497dfb61cede496bdfc1230e1a04172cc7e3852610bc63f049c0caee67e
7
+ data.tar.gz: b86e356de92e63d0a34e1c248bf2abcfd4e50558000f02fb86ef292d666af5a07c6735b23e871b6be85ab5828b0950c9c0ddf66dca98924ace5cb5a60543814e
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ /templates
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.7
7
+ before_install: gem install bundler -v 1.17.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://gems.ruby-china.com/"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in zan_tools.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ zan_tools (0.1.0)
5
+
6
+ GEM
7
+ remote: https://gems.ruby-china.com/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.8.0)
12
+ rspec-core (~> 3.8.0)
13
+ rspec-expectations (~> 3.8.0)
14
+ rspec-mocks (~> 3.8.0)
15
+ rspec-core (3.8.0)
16
+ rspec-support (~> 3.8.0)
17
+ rspec-expectations (3.8.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.8.0)
20
+ rspec-mocks (3.8.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.8.0)
23
+ rspec-support (3.8.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.17)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.0)
32
+ zan_tools!
33
+
34
+ BUNDLED WITH
35
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 zigu
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.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # ZanTools
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/zan_tools`. 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 'zan_tools'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install zan_tools
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at http://gitlab.qima-inc.com/zigu/zan_tools. 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.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "zan_tools"
5
+
6
+ require "irb"
7
+ IRB.start(__FILE__)
data/exe/zan_tools ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require 'zan_tools/command'
5
+
6
+ ZanTools::Command.new.parse!
@@ -0,0 +1,60 @@
1
+ require 'optparse'
2
+ require 'zan_tools/spring_generator'
3
+
4
+ module ZanTools
5
+ class Command
6
+ attr_reader :options
7
+
8
+ def initialize
9
+ @options = {}
10
+ end
11
+
12
+ def parse!
13
+ parser = OptionParser.new do |opts|
14
+ opts.banner = "Usage zan_tools COMMAND [options]"
15
+ opts.separator "COMMAND"
16
+ opts.separator " g/generate: 根据指定模板生成代码"
17
+
18
+ opts.separator "generate options"
19
+ # generate命令参数
20
+ opts.on("-n", "--name NAME", "表名或者类名") do |value|
21
+ options[:name] = value
22
+ end
23
+ opts.on("-f", "--fields FIELDS", "字段列表,格式:字段1:类型2,字段2:类型2") do |value|
24
+ options[:fields] = value.split(",").map{|f| f.split(":") }.select{|a| a.size==2 }.map{|k, v| [k, {'type'=>v}] }
25
+ end
26
+ opts.on("-t", "--template TEMPLATE", "模板文件") do |value|
27
+ if File.exists?(value)
28
+ options[:template] = File.read(value)
29
+ else
30
+ raise "template file not exists"
31
+ end
32
+ end
33
+ # 其他参数
34
+ opts.on("-o", "--output FILE", "输出到指定文件") do |value|
35
+ options[:output] = value
36
+ end
37
+ end
38
+ parser.parse!
39
+
40
+ case ARGV[0]
41
+ when 'g', 'generate'
42
+ generate
43
+ end
44
+ rescue StandardError => e
45
+ puts [e.class, e.message].join(': ')
46
+ end
47
+
48
+ def generate
49
+ raise "generate命令缺失参数" if %w{name fields template}.any?{|f| options[f.to_sym].nil? }
50
+ ZanTools::SpringGenerator.init!(options[:name], options[:fields])
51
+ content = ZanTools::SpringGenerator.compile(options[:template])
52
+
53
+ if options[:output].nil?
54
+ puts content
55
+ else
56
+ File.write(file, content)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,58 @@
1
+ require 'erb'
2
+ require 'zan_tools/string'
3
+
4
+ module ZanTools
5
+ module Generatable
6
+ def self.included(host_class)
7
+ host_class.extend ClassMethods
8
+ end
9
+
10
+ class Configuration
11
+ def initialize
12
+ @config = {}
13
+ end
14
+
15
+ def each(&block)
16
+ @config.each(&block)
17
+ end
18
+
19
+ def method_missing(name, *args)
20
+ matches = name.match(/([^=]+)(=)?/)
21
+ # 判断是否为赋值或取值
22
+ if !matches.nil? && !matches[2].nil? && args.size == 1
23
+ raise "preserved name `#{matches[1]}` is invalid" if respond_to?(matches[1])
24
+ @config[matches[1]] = args[0]
25
+ elsif !matches.nil? && matches[2].nil? && @config.key?(matches[1])
26
+ @config[matches[1]]
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ def compile(tmpl)
33
+ template = ERB.new(tmpl, nil, '-')
34
+ template.result(binding)
35
+ end
36
+ end
37
+
38
+ module ClassMethods
39
+ def config
40
+ @config ||= Configuration.new
41
+ end
42
+
43
+ alias :conf :config
44
+
45
+ def configure
46
+ yield config
47
+ end
48
+
49
+ def define(&block)
50
+ config.instance_eval(&block)
51
+ end
52
+
53
+ def compile(tmpl)
54
+ config.compile(tmpl)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,48 @@
1
+ require 'date'
2
+ require 'zan_tools/string'
3
+ require 'json'
4
+
5
+ module ZanTools
6
+ class Seed
7
+ TEXTS = %Q{12月6日,在结束对西班牙、阿根廷、巴拿马、葡萄牙国事访问并出席二十国集团领导人第十三次峰会后,国家主席习近平回到北京。}.gsub(/[,、。]/, '')
8
+
9
+ def self.generate(fields)
10
+ Hash[fields.map do |name, conf|
11
+ sample = case conf['type'].upper_camel
12
+ when 'Long' then rand(0..1000)
13
+ when 'String' then guess_string(name)
14
+ when 'Date' then DateTime.now.to_s
15
+ when 'Integer' then guess_integer(name)
16
+ when 'Double' then (10000*rand()).to_i/100.0
17
+ else
18
+ conf['type']
19
+ end
20
+ [name, sample]
21
+ end]
22
+ end
23
+
24
+ def self.guess_string(name)
25
+ case name.to_s.downcase
26
+ when /name|title|content|body|desc|comment/
27
+ TEXTS[rand(0 ... TEXTS.size/2) .. rand(TEXTS.size/2 ... TEXTS.size)]
28
+ when /url/i
29
+ "http://www.qima-inc.com/"
30
+ else
31
+ (0.. rand(5..10)).map { (97 + rand(26)).chr }.join
32
+ end
33
+ end
34
+
35
+ def self.guess_integer(name)
36
+ case name.to_s
37
+ when /^(is|has)[_A-Z]/
38
+ rand(0..1)
39
+ else
40
+ rand(0..10)
41
+ end
42
+ end
43
+
44
+ def self.generate_json(fields)
45
+ JSON.generate(generate(fields))
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,58 @@
1
+ require 'zan_tools/generatable'
2
+ require 'zan_tools/seed'
3
+
4
+ module ZanTools
5
+ class SpringGenerator
6
+ include Generatable
7
+
8
+ ID_KEY = 'id'
9
+
10
+ define do
11
+ def id_type # id类型
12
+ fields[ID_KEY]['type']
13
+ end
14
+
15
+ def table_name # 表名
16
+ name.underscore
17
+ end
18
+
19
+ def name_for(ext)
20
+ name + ext
21
+ end
22
+
23
+ # 填充字段内容
24
+ def seeds(ext = {})
25
+ conf = {json: true, exclude: [], camel: true}.merge(ext)
26
+ fs = fields.reject{|k, v| conf[:exclude].include?(k) }
27
+ if conf[:camel]
28
+ fs = Hash[fs.map{|k, v| [k.lower_camel, v]}]
29
+ end
30
+
31
+ ZanTools::Seed.send(conf[:json] ? :generate_json : :generate, fs)
32
+ end
33
+
34
+ def nonid_fields # 非ID字段
35
+ fields.reject{|k, _| k == ID_KEY }
36
+ end
37
+
38
+ def field(name, conf = {})
39
+ fields[name.to_s] = {'type'=>'String'}.merge(Hash[conf.map{|k, v| [k.to_s, v]}])
40
+ end
41
+ end
42
+
43
+ def self.init!(name, fields)
44
+ configure do |config|
45
+ config.name = name.upper_camel
46
+ config.fields = {ID_KEY => {'type'=>'Long'}}
47
+
48
+ fields.each{|name, conf| config.field(name, conf) }
49
+ end
50
+ end
51
+
52
+ def self.belongs_to(generator)
53
+ end
54
+
55
+ def self.has_many(generator)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,27 @@
1
+ class String
2
+ def underscore
3
+ self.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
4
+ end
5
+
6
+ def lower_camel
7
+ gsub(/\A([A-Z])/){|e| $1.downcase }.gsub(/[_]([a-z])/){|e| $1.upcase}
8
+ end
9
+
10
+ def upper_camel
11
+ gsub(/\A(.)/){|e| $1.upcase }.gsub(/[_]([a-z])/){|e| $1.upcase}
12
+ end
13
+
14
+ def to_jdbc
15
+ {
16
+ 'Long' => 'BIGINT',
17
+ 'String' => 'VARCHAR',
18
+ 'Date' => 'TIMESTAMP',
19
+ 'Integer' => 'INTEGER',
20
+ 'Double' => 'DOUBLE'
21
+ }[self] || self.upcase
22
+ end
23
+
24
+ def valid_type?
25
+ %w{Long String Integer Date}.include?(camel)
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module ZanTools
2
+ VERSION = "0.1.0"
3
+ end
data/lib/zan_tools.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "zan_tools/version"
2
+
3
+ module ZanTools
4
+ class Error < StandardError; end
5
+ # Your code goes here...
6
+ end
data/zan_tools.gemspec ADDED
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "zan_tools/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zan_tools"
8
+ spec.version = ZanTools::VERSION
9
+ spec.authors = ["zigu"]
10
+ spec.email = ["zigu@youzan.com"]
11
+
12
+ spec.summary = %q{zan code tools}
13
+ spec.description = %q{zan code tools}
14
+ spec.homepage = "http://gitlab.qima-inc.com/zigu/zan_tools"
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
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "http://gitlab.qima-inc.com/zigu/zan_tools"
24
+ spec.metadata["changelog_uri"] = "http://gitlab.qima-inc.com/zigu/zan_tools"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler", "~> 1.17"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zan_tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - zigu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-06 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: zan code tools
56
+ email:
57
+ - zigu@youzan.com
58
+ executables:
59
+ - zan_tools
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - exe/zan_tools
73
+ - lib/zan_tools.rb
74
+ - lib/zan_tools/command.rb
75
+ - lib/zan_tools/generatable.rb
76
+ - lib/zan_tools/seed.rb
77
+ - lib/zan_tools/spring_generator.rb
78
+ - lib/zan_tools/string.rb
79
+ - lib/zan_tools/version.rb
80
+ - zan_tools.gemspec
81
+ homepage: http://gitlab.qima-inc.com/zigu/zan_tools
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ allowed_push_host: https://rubygems.org/
86
+ homepage_uri: http://gitlab.qima-inc.com/zigu/zan_tools
87
+ source_code_uri: http://gitlab.qima-inc.com/zigu/zan_tools
88
+ changelog_uri: http://gitlab.qima-inc.com/zigu/zan_tools
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.7.8
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: zan code tools
109
+ test_files: []