ratbug 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
+ SHA256:
3
+ metadata.gz: f8455d102f08a34f1a91cbb925729a7e8a1fac0d94bb5967156c67253c1db8ff
4
+ data.tar.gz: ce78a895586d2b98962e6560719fffbdfd3aef4165feceaf8ee79e259d68c275
5
+ SHA512:
6
+ metadata.gz: bb75ca8bf44fc5e1b9211e3b0ba435376daf2e4ed1424c9d69bc7aee6f9c024d23f09be5db09d2e2d7a1ba77e70bf174bbe7c0b0efef29c4767e0f245e2d6e6a
7
+ data.tar.gz: 2075780d7cfe7eb60a40a242bd8be1aec8e723d0b096326f32877875fe42ae3dce5d43f65ac3ebf81e83c0bdbbff85470d6e346ce6884c1b18464632ad4e159d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
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
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -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 attinyes@gmail.com. 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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ratbug.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ratbug (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (12.3.3)
11
+ rspec (3.10.0)
12
+ rspec-core (~> 3.10.0)
13
+ rspec-expectations (~> 3.10.0)
14
+ rspec-mocks (~> 3.10.0)
15
+ rspec-core (3.10.1)
16
+ rspec-support (~> 3.10.0)
17
+ rspec-expectations (3.10.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.10.0)
20
+ rspec-mocks (3.10.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.10.0)
23
+ rspec-support (3.10.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ rake (~> 12.0)
30
+ ratbug!
31
+ rspec (~> 3.0)
32
+
33
+ BUNDLED WITH
34
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 rnitta
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,54 @@
1
+ # Ratbug
2
+ Ratbug (Rails and typescript building utilities generator)
3
+
4
+ Rails plugin to load schema and models, and generate typescript type(or interface) definitions and [jbuilder](https://github.com/rails/jbuilder) templates.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'ratbug'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ And Then generate rake task on your environment.
19
+
20
+ ```
21
+ $ rails g ratbug:install
22
+ ```
23
+
24
+ ## memo
25
+ schema.rb must be present
26
+ sql type schema is another option, but this tool is for schema.rb.
27
+
28
+ ## Usage
29
+
30
+ ```shell
31
+ # $ rails ratbug:execute[<table_names>]
32
+ $ rails ratbug:execute[users]
33
+ ```
34
+
35
+ then `tmp/ratbug/_user.jbuilder` and `tmp/ratbug/_user.ts` will be generated.
36
+
37
+ ## Development
38
+
39
+ 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.
40
+
41
+ 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).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ratbug. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ratbug/blob/master/CODE_OF_CONDUCT.md).
46
+
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
51
+
52
+ ## Code of Conduct
53
+
54
+ Everyone interacting in the Ratbug project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ratbug/blob/master/CODE_OF_CONDUCT.md).
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/ratbug ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # unless File.exist?('./Gemfile')
4
+ # abort 'Please run ratbug from the root of the project.'
5
+ # end
6
+ # pp p p p p p p p p pp p pp 2
7
+ # require 'rubygems'
8
+ # begin
9
+ # require 'bundler'
10
+ # Bundler.setup
11
+ # rescue StandardError
12
+ # end
13
+ #
14
+ # require 'ratbug'
15
+ # using ActiveRecordSchemaPatcher
16
+ # load(Rails.root.join('db', 'schema.rb'))
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate ratbug Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,11 @@
1
+ module Ratbug
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('templates', __dir__)
5
+
6
+ def copy_tasks
7
+ template 'ratbug.rake', 'lib/tasks/ratbug.rake'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ if Rails.env.development?
2
+ namespace :ratbug do
3
+ task execute: :environment do |_, args|
4
+ require 'ratbug'
5
+ table_names = args.extras
6
+ Rails.application.eager_load! # table_name_to_model_converter
7
+ options = Ratbug::GenerateConfig.new(
8
+ enabled_outputs: ['jbuilder', 'typescript'],
9
+ ts_prefer_type: true,
10
+ ts_enum_output: 'union_type',
11
+ ts_prefer_undefined: true,
12
+ use_only_schema: false,
13
+ output_dir: Rails.root.join('tmp', 'ratbug'),
14
+ table_name_to_model_converter: -> table_name {
15
+ ApplicationRecord.descendants.find { |klass| klass.table_name == table_name }
16
+ }
17
+ ).options
18
+ table_names.each do |table_name|
19
+ Ratbug::Runner.execute(table_name, options)
20
+ end
21
+ end
22
+ end
23
+ end
data/lib/ratbug.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'ratbug/generate_config'
2
+ require 'ratbug/runner'
3
+ require 'ratbug/version'
4
+ require 'ratbug/active_record_schema_patcher'
5
+ require 'ratbug/active_record_schema_monkey'
6
+
7
+ module Ratbug
8
+ end
@@ -0,0 +1,36 @@
1
+ require 'ratbug/table'
2
+
3
+ module Ratbug
4
+ module ActiveRecordSchemaMonkeyExt
5
+ def create_table(table_name, **, &block)
6
+ table = Table.new(table_name)
7
+ table.instance_exec(table, &block)
8
+ @tables[table_name] = table
9
+ end
10
+
11
+ def enable_extension(*)
12
+ # do nothing
13
+ end
14
+
15
+ def add_foreign_key(*)
16
+ # do nothing
17
+ end
18
+ end
19
+
20
+ class ActiveRecordSchemaMonkey
21
+ attr_accessor :tables
22
+
23
+ def initialize
24
+ # {table_name: Table}
25
+ @tables = Hash.new
26
+ end
27
+
28
+ private
29
+
30
+ include ActiveRecordSchemaMonkeyExt
31
+
32
+ def method_missing(symbol, *args)
33
+ puts "ActiveRecord::Schema##{symbol.to_s}(#{args.join(', ')}) has no use currently"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ require 'ratbug/active_record_schema_monkey'
2
+
3
+ module Ratbug
4
+ module ActiveRecordSchemaPatcher
5
+ refine ActiveRecord::Schema do
6
+ class << ActiveRecord::Schema
7
+ def define(**argh, &block)
8
+ p argh
9
+ monkey = ActiveRecordSchemaMonkey.new
10
+ monkey.instance_eval(&block)
11
+ monkey
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,45 @@
1
+ module Ratbug
2
+ class Column
3
+ # enum: Hash<String, Integer>
4
+ attr_accessor :type, :name, :nullable, :comment, :enum
5
+
6
+ VALID_COLUMN_TYPES = %i[
7
+ bigint
8
+ binary
9
+ boolean
10
+ date
11
+ datetime
12
+ decimal
13
+ float
14
+ integer
15
+ json
16
+ primary_key
17
+ string
18
+ text
19
+ time
20
+ ].freeze
21
+
22
+ # @param [Symbol] type
23
+ # @param [String] name
24
+ # @param [boolean] nullable
25
+ # @param [String | null] comment
26
+ def initialize(type, name, nullable, comment)
27
+ fail "column type #{type} is invalid" unless VALID_COLUMN_TYPES.include?(type)
28
+ fail "column name is required" if name.blank?
29
+
30
+ @type = type
31
+ @name = name
32
+ @nullable = nullable
33
+ @comment = comment
34
+ end
35
+
36
+ # @param [Hash<String, Integer>] enum_hash
37
+ def set_enums(enum_hash)
38
+ @enum = enum_hash
39
+ end
40
+
41
+ def set_nullable(nullable)
42
+ @nullable = nullable
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,55 @@
1
+ module Ratbug
2
+ class GenerateConfig
3
+ VALID_OPTION_KEYS = %i[
4
+ enabled_outputs
5
+ ts_prefer_type
6
+ ts_enum_output_type
7
+ ts_prefere_undefined
8
+ use_only_schema
9
+ output_dir
10
+ table_name_to_model_converter
11
+ ].freeze
12
+
13
+ # timestamp
14
+ # jbuilder indent usetab
15
+
16
+ attr_accessor :override_options
17
+
18
+ def initialize(**options)
19
+ @override_options = {}
20
+ options.each do |key, value|
21
+ set(key, value)
22
+ end
23
+ end
24
+
25
+ def set(key, value)
26
+ key = key.to_sym
27
+ puts "warning: GenerateConfig option #{key} has duplicated." unless @override_options[key].nil?
28
+ @override_options[key] = value
29
+ self
30
+ end
31
+
32
+ # @return [Hash<Symbol, Object>]
33
+ def options
34
+ {
35
+ **default_options,
36
+ **@override_options
37
+ }
38
+ end
39
+
40
+ private
41
+
42
+ # @return [Hash<Symbol, Object>]
43
+ def default_options
44
+ {
45
+ enabled_outputs: ['jbuilder', 'typescript'],
46
+ ts_prefer_type: true, # type or interface
47
+ ts_enum_output: 'union_type', # 'enum'
48
+ ts_prefere_undefined: true, # or null
49
+ use_only_schema: false, # scan model files
50
+ output_dir: Rails.root.join('tmp', 'ratbug'),
51
+ table_name_to_model_converter: -> table_name { table_name.singularize.camelize.constantize }
52
+ }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,57 @@
1
+ require 'fileutils'
2
+
3
+ module Ratbug
4
+ module Generators
5
+ class JbuilderGenerator
6
+ def initialize(table, options)
7
+ @table = table
8
+ @options = options
9
+ end
10
+
11
+ def generate
12
+ singular = @table.name.singularize
13
+ output = ""
14
+ output << "json.#{singular} do |#{singular}|\n"
15
+ @table.columns.values.sort_by(&:name).each do |column|
16
+ output << column_row(column, singular)
17
+ end
18
+ output << "end"
19
+
20
+ dirname = File.dirname(output_path)
21
+ unless File.directory?(dirname)
22
+ FileUtils.mkdir_p(dirname)
23
+ end
24
+
25
+ File.open(output_path, "w") do |f|
26
+ f.puts(output)
27
+ end
28
+ puts "generated: #{output_path}"
29
+ end
30
+
31
+ private
32
+
33
+ def column_row(column, receiver_name)
34
+ " json.#{column.name} #{receiver_name}.#{column.name}#{column_value_modifier(column)}\n"
35
+ end
36
+
37
+ def column_value_modifier(column)
38
+ if column.enum.present?
39
+ return ''
40
+ end
41
+
42
+ operator = column.nullable ? '&' : ''
43
+
44
+ case column.type
45
+ when :date, :datetime, :time then
46
+ return "#{operator}.iso8601"
47
+ else
48
+ return ''
49
+ end
50
+ end
51
+
52
+ def output_path
53
+ @options[:output_dir].join("_#{@table.name.singularize}.jbuilder")
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,93 @@
1
+ require 'fileutils'
2
+
3
+ module Ratbug
4
+ module Generators
5
+ class TypescriptGenerator
6
+ def initialize(table, options)
7
+ @table = table
8
+ @options = options
9
+ end
10
+
11
+ def generate
12
+ output = ""
13
+ @table.columns.values.filter { |c| c.enum.present? }.each do |column|
14
+ output << enum_output(column)
15
+ end
16
+
17
+ output << "type #{@table.name.singularize.camelize} = {\n"
18
+ @table.columns.values.sort_by(&:name).each do |column|
19
+ output << column_row(column)
20
+ end
21
+ output << "};"
22
+
23
+ dirname = File.dirname(output_path)
24
+ unless File.directory?(dirname)
25
+ FileUtils.mkdir_p(dirname)
26
+ end
27
+ File.open(output_path, "w") do |f|
28
+ f.puts(output)
29
+ end
30
+
31
+ puts "generated: #{output_path}"
32
+ end
33
+
34
+ private
35
+
36
+ def column_row(column)
37
+ ret = " #{column.name.camelize(:lower)}#{column.nullable ? '?' : ''}:"
38
+ if column.enum.present?
39
+ ret << " #{enum_type_name(column)};\n"
40
+ else
41
+ ret << " #{column_type_to_ts_type(column.type)};\n"
42
+ end
43
+ ret
44
+ end
45
+
46
+ def enum_output(column)
47
+ type_name = enum_type_name(column)
48
+ output = ""
49
+ if @options[:ts_enum_output] === 'union_type'
50
+ output << "type #{type_name} = "
51
+ output << column.enum.keys.map do |enum_key|
52
+ "'#{enum_key}'"
53
+ end.join(' | ')
54
+ output << ';'
55
+ else
56
+ # force string enum
57
+ output << "enum #{type_name} {\n"
58
+ column.enum.keys.map do |enum_key|
59
+ output << "#{enum_key.camelize} = '#{enum_key}',\n"
60
+ end.join(' | ')
61
+ output << "}"
62
+ end
63
+ output << "\n\n"
64
+ end
65
+
66
+ def enum_type_name(column)
67
+ "#{@table.name}_#{column.name}".pluralize.camelize
68
+ end
69
+
70
+ def column_type_to_ts_type(column_type)
71
+ {
72
+ bigint: 'number',
73
+ binary: 'number',
74
+ boolean: 'boolean',
75
+ date: 'string',
76
+ datetime: 'string',
77
+ decimal: 'number',
78
+ float: 'string',
79
+ integer: 'number',
80
+ json: 'string',
81
+ primary_key: 'number',
82
+ string: 'string',
83
+ text: 'string',
84
+ time: 'string'
85
+ }[column_type.to_sym]
86
+ end
87
+
88
+ def output_path
89
+ @options[:output_dir].join("_#{@table.name.singularize}.ts")
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,54 @@
1
+ require 'ratbug/active_record_schema_patcher'
2
+ require 'ratbug/generators/jbuilder_generator'
3
+ require 'ratbug/generators/typescript_generator'
4
+
5
+ module Ratbug
6
+ class Runner
7
+ class << self
8
+ using Ratbug::ActiveRecordSchemaPatcher
9
+
10
+ # @param [Array<String>] table_names
11
+ # @param [GenerateConfig] options
12
+ def execute(table_names, options)
13
+ unless defined? Rails
14
+ fail 'this gem is to be used in rails context'
15
+ end
16
+ # scan schema
17
+ monkey = eval(File.open(Rails.root.join('db', 'schema.rb')).read)
18
+ tables = monkey.tables.slice(*table_names)
19
+ tables.values.each do |table|
20
+ unless options[:use_only_schema]
21
+ model = table_name_to_model(table.name, options[:table_name_to_model_converter])
22
+ if model.present?
23
+ table.load_enums(model)
24
+ table.load_presence_validators(model)
25
+ end
26
+ end
27
+
28
+ if options[:enabled_outputs].include?('jbuilder')
29
+ Ratbug::Generators::JbuilderGenerator.new(table, options).generate
30
+ end
31
+
32
+ if options[:enabled_outputs].include?('typescript')
33
+ Ratbug::Generators::TypescriptGenerator.new(table, options).generate
34
+ end
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ # @param [String] table_names
41
+ # @param [Proc] converter table_name to ApplicationRecord converting Proc
42
+ # @return [ApplicationRecord|NilClass]
43
+ def table_name_to_model(table_name, converter)
44
+ converter[table_name]
45
+ rescue => e
46
+ p e
47
+ puts "table_name: #{table_name}, cannot find the corresponding model."
48
+ nil
49
+ end
50
+
51
+ end
52
+ end
53
+ end
54
+
@@ -0,0 +1,69 @@
1
+ require 'ratbug/column'
2
+
3
+ module Ratbug
4
+ module TableExt
5
+ def index(*,**)
6
+ # do nothing
7
+ end
8
+
9
+ def timestamps(*,**)
10
+ columns[:created_at] = Column.new(:datetime, 'created_at', false, nil)
11
+ columns[:updated_at] = Column.new(:datetime, 'updated_at', false, nil)
12
+ end
13
+
14
+ Column::VALID_COLUMN_TYPES.filter { |r| r != :timestamps }.each do |type|
15
+ define_method(type, -> (column_name, **options) {
16
+ comment = options['comment'] || options[:comment]
17
+ nullable = !(options['null'] == false || options[:null] == false)
18
+ columns[column_name.to_sym] = Column.new(type, column_name.to_s, nullable, comment)
19
+ })
20
+ end
21
+ end
22
+
23
+
24
+ class Table
25
+ attr_accessor :name, :columns
26
+
27
+ def initialize(name)
28
+ @name = name
29
+ @columns = {}
30
+ end
31
+
32
+ # @param [ApplicationRecord] model
33
+ def load_enums(model)
34
+ model.defined_enums.each do |column_name, v|
35
+ if v.is_a?(Hash)
36
+ enum_values = v
37
+ elsif v.is_a?(Array)
38
+ enum_values = v.map.with_index(0) { |r, i| [r, i] }.to_h
39
+ else
40
+ fail "#{model.name} enum #{column_name.pluralize} is not valid"
41
+ end
42
+ columns[column_name.to_sym]&.set_enums(enum_values)
43
+ end
44
+ end
45
+
46
+ def load_relations
47
+ # TODO
48
+ end
49
+
50
+ # @param [ApplicationRecord] model
51
+ def load_presence_validators(model)
52
+ model.validators.filter { |r| r.is_a?(ActiveRecord::Validations::PresenceValidator) }.each do |validator|
53
+ validator.attributes.each do |column_name_sym|
54
+ columns[column_name_sym]&.set_nullable(false)
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ include TableExt
62
+
63
+ def method_missing(symbol, *argv, **argh)
64
+ puts "t.#{symbol.to_s} has no correspond implementation currenlty"
65
+ p argv
66
+ p argh
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module Ratbug
2
+ VERSION = '0.0.1'
3
+ end
data/ratbug.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/ratbug/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ github_repo_url = 'https://github.com/rnitta/ratbug'
5
+ spec.name = "ratbug"
6
+ spec.version = Ratbug::VERSION
7
+ spec.authors = ["rnitta"]
8
+ spec.email = ["attinyes@gmail.com"]
9
+
10
+ spec.summary = %q{Generate typescript type definitions and jbuilder templates from schema.rb and model files.}
11
+ spec.description = %q{poor utility}
12
+ spec.homepage = github_repo_url
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
15
+
16
+ spec.metadata["homepage_uri"] = github_repo_url
17
+ spec.metadata["source_code_uri"] = github_repo_url
18
+ spec.metadata["changelog_uri"] = "#{github_repo_url}/releases"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.executables = ["ratbug"]
26
+ spec.bindir = "bin"
27
+ spec.require_paths = ["lib"]
28
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ratbug
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - rnitta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: poor utility
14
+ email:
15
+ - attinyes@gmail.com
16
+ executables:
17
+ - ratbug
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/ratbug
30
+ - lib/generators/ratbug/USAGE
31
+ - lib/generators/ratbug/install_generator.rb
32
+ - lib/generators/ratbug/templates/ratbug.rake
33
+ - lib/ratbug.rb
34
+ - lib/ratbug/active_record_schema_monkey.rb
35
+ - lib/ratbug/active_record_schema_patcher.rb
36
+ - lib/ratbug/column.rb
37
+ - lib/ratbug/generate_config.rb
38
+ - lib/ratbug/generators/jbuilder_generator.rb
39
+ - lib/ratbug/generators/typescript_generator.rb
40
+ - lib/ratbug/runner.rb
41
+ - lib/ratbug/table.rb
42
+ - lib/ratbug/version.rb
43
+ - ratbug.gemspec
44
+ homepage: https://github.com/rnitta/ratbug
45
+ licenses:
46
+ - MIT
47
+ metadata:
48
+ homepage_uri: https://github.com/rnitta/ratbug
49
+ source_code_uri: https://github.com/rnitta/ratbug
50
+ changelog_uri: https://github.com/rnitta/ratbug/releases
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.5.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.1.2
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Generate typescript type definitions and jbuilder templates from schema.rb
70
+ and model files.
71
+ test_files: []