schema2type 0.2.2 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d907bb961b8e4dd1d7c94732012dc0aafd75c4e29aef02027ef5b5c7751889f
4
- data.tar.gz: 6494b9e501419c0f9a300ff5b2d056f9224296ddb262b4f709337dd53cabe765
3
+ metadata.gz: 66c06b1860fe83c6983e2c0249715531317885d9c62616908afc9e02d56eaa3f
4
+ data.tar.gz: afa4290333490132677c1da1387d5b7905a09541e5150addc5bdf5c3b97a8698
5
5
  SHA512:
6
- metadata.gz: 92b851eb75ea3a0a08bbb9a1e9d9f9c097d47b5d81b8d129afc396cd5f9cc27f7e1d8958a702e167457f9adf6c7b8044cd07d4c0a99f59422e5e7b7d3f4c1320
7
- data.tar.gz: 8e4cec8d0f67e5835ff6dd7c28a457b364c27458f9ce1734a211961ffb28772b726183d990fcbc6682c56cddca568900a51361df75b859b5c2dcaf338ccfec95
6
+ metadata.gz: 653ce6ec1e4cf47eeb7f79c70ea9da58d348f8cc401949e567e6c8bd807897e6e5a4fbb770b28f98e215ae2abfc2ef71e688227f556df67daefd2e8d07af6017
7
+ data.tar.gz: d050b51ab3cf3be417eedba84208cd35a98fa68a15ca89a509c3730392345dac06d71d8bccffdcefeeb1742c5b1b205d6f22b55b0cf72353207ab37787dbc020
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.0
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source "https://rubygems.org"
2
+ ruby '2.7.0'
2
3
 
3
4
  # Specify your gem's dependencies in schema2type.gemspec
4
5
  gemspec
data/Gemfile.lock CHANGED
@@ -1,39 +1,41 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- schema2type (0.2.2)
4
+ schema2type (0.3.0)
5
5
  activesupport
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (5.2.2)
10
+ activesupport (6.0.3.1)
11
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
12
12
  i18n (>= 0.7, < 2)
13
13
  minitest (~> 5.1)
14
14
  tzinfo (~> 1.1)
15
- concurrent-ruby (1.1.4)
15
+ zeitwerk (~> 2.2, >= 2.2.2)
16
+ concurrent-ruby (1.1.6)
16
17
  diff-lcs (1.3)
17
- i18n (1.5.3)
18
+ i18n (1.8.3)
18
19
  concurrent-ruby (~> 1.0)
19
- minitest (5.11.3)
20
- rake (10.5.0)
21
- rspec (3.8.0)
22
- rspec-core (~> 3.8.0)
23
- rspec-expectations (~> 3.8.0)
24
- rspec-mocks (~> 3.8.0)
25
- rspec-core (3.8.0)
26
- rspec-support (~> 3.8.0)
27
- rspec-expectations (3.8.2)
20
+ minitest (5.14.1)
21
+ rake (13.0.1)
22
+ rspec (3.9.0)
23
+ rspec-core (~> 3.9.0)
24
+ rspec-expectations (~> 3.9.0)
25
+ rspec-mocks (~> 3.9.0)
26
+ rspec-core (3.9.2)
27
+ rspec-support (~> 3.9.3)
28
+ rspec-expectations (3.9.2)
28
29
  diff-lcs (>= 1.2.0, < 2.0)
29
- rspec-support (~> 3.8.0)
30
- rspec-mocks (3.8.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-mocks (3.9.1)
31
32
  diff-lcs (>= 1.2.0, < 2.0)
32
- rspec-support (~> 3.8.0)
33
- rspec-support (3.8.0)
33
+ rspec-support (~> 3.9.0)
34
+ rspec-support (3.9.3)
34
35
  thread_safe (0.3.6)
35
- tzinfo (1.2.5)
36
+ tzinfo (1.2.7)
36
37
  thread_safe (~> 0.1)
38
+ zeitwerk (2.3.0)
37
39
 
38
40
  PLATFORMS
39
41
  ruby
@@ -41,9 +43,12 @@ PLATFORMS
41
43
  DEPENDENCIES
42
44
  activesupport
43
45
  bundler (~> 2.0)
44
- rake (~> 10.0)
46
+ rake (~> 13.0)
45
47
  rspec (~> 3.0)
46
48
  schema2type!
47
49
 
50
+ RUBY VERSION
51
+ ruby 2.7.0p0
52
+
48
53
  BUNDLED WITH
49
- 2.0.1
54
+ 2.1.4
data/README.md CHANGED
@@ -22,12 +22,13 @@ Automatically have the following TypesScript type generated.
22
22
  ```typescript
23
23
  declare namespace schema {
24
24
  type User = {
25
- name: string | null
26
- age: number | null
27
- sales: number | null
28
- paid: boolean
29
- createdAt: Date
30
- updatedAt: Date
25
+ id: number;
26
+ name: string | null;
27
+ age: number | null;
28
+ sales: number | null;
29
+ paid: boolean;
30
+ createdAt: string;
31
+ updatedAt: string;
31
32
  }
32
33
  }
33
34
  ```
@@ -81,10 +82,10 @@ the schema2type convert as per this conversion table.
81
82
  | float | number |
82
83
  | decimal | number |
83
84
  | boolean | boolean |
84
- | date | Date |
85
- | datetime | Date |
86
- | timestamp | Date |
87
- | datetime_with_timezone | Date |
85
+ | date | string |
86
+ | datetime | string |
87
+ | timestamp | string |
88
+ | datetime_with_timezone | string |
88
89
  ## License
89
90
 
90
91
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/exe/schema2type CHANGED
@@ -5,4 +5,4 @@ require 'schema2type'
5
5
 
6
6
  params = ARGV.getopts('s:o:n:', 'snake')
7
7
 
8
- Schema2type.execute(input_file: params['s'], out_file: params['o'], name_space: params['n'], snake_case: params['snake'])
8
+ Schema2type.execute(input_file: params['s'], out_file: params['o'], name_space: params['n'], is_snake_case: params['snake'])
@@ -1,11 +1,9 @@
1
1
  module Schema2type
2
-
3
2
  DEFAULT_SCHEMA_PATH = "./db/schema.rb".freeze
4
3
  DEFAULT_NAME_SPACE = "schema".freeze
5
4
 
6
- def self.execute(input_file:, out_file:, name_space:, snake_case:)
7
-
8
- result = eval(File.read(input_file || DEFAULT_SCHEMA_PATH), CovertService.new(snake_case).get_binding)
5
+ def self.execute(input_file:, out_file:, name_space:, is_snake_case:)
6
+ resultHash = eval(File.read(input_file || DEFAULT_SCHEMA_PATH), CovertService.new(is_snake_case).get_binding)
9
7
 
10
8
  File.open(out_file, "w") do |f|
11
9
  f.puts <<~EOS
@@ -13,11 +11,11 @@ module Schema2type
13
11
 
14
12
  /**
15
13
  * auto-generated file
16
- * schema version: #{result[:version]}
14
+ * schema version: #{resultHash[:version]}
17
15
  * This file was automatically generated by schema2type
18
16
  */
19
17
  declare namespace #{name_space || DEFAULT_NAME_SPACE} {
20
- #{result[:lines]}
18
+ #{resultHash[:lines]}
21
19
  }
22
20
  EOS
23
21
  end
@@ -10,7 +10,7 @@ bigint: number
10
10
  float: number
11
11
  decimal: number
12
12
  boolean: boolean
13
- date: Date
14
- datetime: Date
15
- timestamp: Date
16
- datetime_with_timezone: Date
13
+ date: string
14
+ datetime: string
15
+ timestamp: string
16
+ datetime_with_timezone: string
@@ -1,28 +1,36 @@
1
1
  module Schema2type
2
2
  class CovertService
3
- def initialize(snake_case)
3
+ def initialize(is_snake_case)
4
4
  @converted_types = []
5
- @snake_case = snake_case
5
+ @is_snake_case = is_snake_case
6
6
  end
7
7
 
8
8
  def get_binding
9
9
  binding
10
10
  end
11
11
 
12
- def create_table(table_name, *)
13
- converter = SchemaConverter.new(table_name: table_name, snake_case: @snake_case)
12
+ # mock method for create_table in schema.rb
13
+ def convert_schema_to_type(table_name, *)
14
+ converter = SchemaConverter.new(table_name: table_name, is_snake_case: @is_snake_case)
14
15
  yield converter
15
- @converted_types.concat converter.result
16
+ @converted_types.concat converter.converted_type_lines
17
+ end
18
+
19
+ def skip_dsl(*)
20
+ @converted_types
16
21
  end
17
22
 
18
23
  def method_missing(*)
19
24
  # To exclude unnecessary methods
25
+ # TODO: add error handling
20
26
  end
21
27
 
22
28
  def self.method_missing(*)
23
29
  # To exclude unnecessary methods
30
+ # TODO: add error handling
24
31
  end
25
32
 
33
+ # mock module and method for shcema.rb
26
34
  module ActiveRecord
27
35
  module Schema
28
36
  def self.define(*arg)
@@ -35,5 +43,8 @@ module Schema2type
35
43
  end
36
44
  end
37
45
  end
46
+
47
+ alias create_table convert_schema_to_type
48
+ alias add_foreign_key skip_dsl
38
49
  end
39
50
  end
@@ -3,43 +3,41 @@ require 'active_support/inflector'
3
3
 
4
4
  module Schema2type
5
5
  class SchemaConverter
6
- attr_reader :property_lines, :table_name, :snake_case
6
+ attr_reader :property_lines, :table_name, :is_snake_case
7
7
 
8
- TYPE_STRING = 'string'.freeze
9
- TYPE_NUMBER = 'number'.freeze
10
- TYPE_BOOLEAN = 'boolean'.freeze
11
- TYPE_DATE = 'Date'.freeze
12
8
  COLUMN_METHODS = YAML.load_file(File.expand_path(__dir__) + '/conversion_table.yml').to_a
9
+ ID_PROPERTY_LINE_TEXT = " id: number;".freeze
13
10
 
14
11
  def self.define_convert_methods(methods)
15
12
  methods.each do |m|
16
13
  define_method(m[0]) do |name, *options|
17
- push_property_line name: name, type: m[1], options: options
14
+ convert_property_line_and_push name: name, type: m[1], options: options
18
15
  end
19
16
  end
20
17
  end
21
18
 
22
19
  define_convert_methods COLUMN_METHODS
23
20
 
24
- def initialize(table_name:, snake_case: false)
21
+ def initialize(table_name:, is_snake_case: false)
25
22
  @property_lines = []
26
23
  @table_name = table_name.singularize.camelize
27
- @snake_case = snake_case
24
+ @is_snake_case = is_snake_case
28
25
  end
29
26
 
30
- def result
31
- ["type #{table_name} = {", property_lines, "}\n"].flatten
27
+ def converted_type_lines
28
+ ["type #{table_name} = {", ID_PROPERTY_LINE_TEXT, property_lines, "}\n"].flatten
32
29
  end
33
30
 
34
31
  def method_missing(*)
35
32
  # To exclude unnecessary methods
33
+ # TODO: add error handling
36
34
  end
37
35
 
38
36
  private
39
37
 
40
- def push_property_line(name:, type:, options:)
38
+ def convert_property_line_and_push(name:, type:, options:)
41
39
  is_non_nullable = options[0] && options[0].key?(:null) && !options[0][:null]
42
- formatted_name = snake_case ? name.underscore : name.camelcase(:lower)
40
+ formatted_name = is_snake_case ? name.underscore : name.camelcase(:lower)
43
41
  property_line = is_non_nullable ? "#{formatted_name}: #{type};" : "#{formatted_name}: #{type} | null;"
44
42
 
45
43
  property_lines << " #{property_line}"
@@ -1,3 +1,3 @@
1
1
  module Schema2type
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.1'
3
3
  end
data/schema2type.gemspec CHANGED
@@ -24,6 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.require_paths = ["lib"]
25
25
  spec.add_runtime_dependency "activesupport"
26
26
  spec.add_development_dependency "bundler", "~> 2.0"
27
- spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rake", "~> 13.0"
28
28
  spec.add_development_dependency "rspec", "~> 3.0"
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema2type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ryo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-22 00:00:00.000000000 Z
11
+ date: 2021-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -77,6 +77,7 @@ extra_rdoc_files: []
77
77
  files:
78
78
  - ".gitignore"
79
79
  - ".rspec"
80
+ - ".ruby-version"
80
81
  - ".travis.yml"
81
82
  - CODE_OF_CONDUCT.md
82
83
  - Gemfile
@@ -113,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
114
  - !ruby/object:Gem::Version
114
115
  version: '0'
115
116
  requirements: []
116
- rubygems_version: 3.0.2
117
+ rubygems_version: 3.1.2
117
118
  signing_key:
118
119
  specification_version: 4
119
120
  summary: generate TypeScript type definitions from Rails schema.rb