schema2type 0.1.12 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a45c45028c0a021fe9a694ee6daeae17b1fed99d62b2049a78ace7759cd85194
4
- data.tar.gz: d6ed16bc7551ffa35a55bc5ec70060228621da725821717eccab9821a1bbada0
3
+ metadata.gz: 9c3e52c8948ce5f2642167c3fe437b480c2a8ec47e83c1cf9c25a2e555ee205b
4
+ data.tar.gz: 38d1d525a6227191010dd1eb280c5fa10781df2bce44ae9b86b839dc98e5174e
5
5
  SHA512:
6
- metadata.gz: a8400cfa1ff3dc05f746134d3b141424860e952b963779efa21f6629b28ff8a6b38890d56e04e8e921651a54ae9903182919fe26fd894e8817c244b4a558f242
7
- data.tar.gz: 83311f3ce07b9861d58ad0686b7de5b59f0f31ebeaa3ef852146a2cde6d5e325a191f3ac760230ad53db4d0997381f40b84c0e6767ac036274112d410da8b9ab
6
+ metadata.gz: cba2e579992d67be775d58d2b33a9aa63161689e10a1dc2ddbd3ddbf1b7709128ffa899cb9db732220f0965a3947808d35fbf7c1256e88d426dcf5e1c145a78f
7
+ data.tar.gz: a5c22fe47056ac52ce8cfd4d40965950a50e575d41bcc5d0d85adbdaa9701fed96cda6609cc42d2b66a5d86c69b71d23e41817873424f908def7029f06a6cab1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- schema2type (0.1.11)
4
+ schema2type (0.1.12)
5
5
  activesupport
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -17,7 +17,7 @@ ActiveRecord::Schema.define(version: xxxx) do
17
17
  end
18
18
  ```
19
19
 
20
- Automatically have the following TypesScript type generated
20
+ Automatically have the following TypesScript type generated.
21
21
 
22
22
  ```typescript
23
23
  declare namespace schema {
@@ -53,19 +53,20 @@ Or install it yourself as:
53
53
  ## Usage
54
54
 
55
55
  ```
56
- bundle exec schema2type -s db/schema.rb -o schema.d.ts -n schema
56
+ bundle exec schema2type -o schema.d.ts
57
57
  ```
58
58
 
59
- #### options
59
+ ### options
60
60
 
61
61
  |command | require | default | detail |
62
62
  |---|---|---|---|
63
- | -s | true | - | Path of your schema.rb |
64
63
  | -o | true | - | Output file name of TypeScript |
64
+ | -s | false | "./db/schema.rb" | Path of your schema.rb |
65
65
  | -n | false | "schema" | Name of declare namespace |
66
66
  | --snake | false | false | Convert property name to snake_case |
67
67
 
68
- #### conversion table
68
+ ## conversion table
69
+ the schema2type convert as per this conversion table.
69
70
 
70
71
  |create_table block method| converted Type|
71
72
  |---|---|
@@ -1,34 +1,38 @@
1
1
  module Schema2type
2
2
 
3
+ DEFAULT_SCHEMA_PATH = "./db/schema.rb"
4
+ DEFAULT_NAME_SPACE = "schema"
5
+
3
6
  def self.execute(input_file:, out_file:, name_space:, snake_case:)
4
- @@convert_types = []
7
+ @@converted_types = []
5
8
  @@snake_case = snake_case
6
- eval(File.read(input_file))
7
9
 
8
- convert_type_text = @@convert_types.map { |t| " #{t}" }.join("\n").strip
10
+ eval(File.read(input_file || DEFAULT_SCHEMA_PATH))
11
+
12
+ convert_type_text = @@converted_types.map { |t| " #{t}" }.join("\n").strip
9
13
 
10
14
  File.open(out_file, "w") do |f|
11
- f.puts <<-EOS
12
- /* eslint no-unused-vars: 0 */
13
-
14
- /**
15
- * auto-generated file
16
- * schema version: #{$schema_version}
17
- * This file was automatically generated by schema2type
18
- */
19
- declare namespace #{name_space ||= "schema"} {
20
- #{convert_type_text}
21
- }
22
- EOS
15
+ f.puts <<~EOS
16
+ /* eslint no-unused-vars: 0 */
17
+
18
+ /**
19
+ * auto-generated file
20
+ * schema version: #{$schema_version}
21
+ * This file was automatically generated by schema2type
22
+ */
23
+ declare namespace #{name_space || DEFAULT_NAME_SPACE} {
24
+ #{convert_type_text}
25
+ }
26
+ EOS
23
27
  end
24
28
  end
25
29
 
26
- def self.create_table(table_name, *arg, &block)
30
+ def self.convert_schema(table_name, *)
27
31
  converter = SchemaConverter.new(table_name: table_name, snake_case: @@snake_case)
28
- block.call(converter)
29
- converter.finalize
30
32
 
31
- @@convert_types.concat(converter.out_text)
33
+ yield converter
34
+
35
+ @@converted_types.concat converter.result
32
36
  end
33
37
 
34
38
  def self.method_missing(*arg)
@@ -36,11 +40,15 @@ EOS
36
40
  end
37
41
 
38
42
  module ActiveRecord
39
- class Schema
40
- def self.define(version, &block)
43
+ module Schema
44
+ def self.define(version)
41
45
  $schema_version = version[:version]
42
- block.call
46
+ yield
43
47
  end
44
48
  end
45
49
  end
50
+
51
+ class << self
52
+ alias :create_table :convert_schema
53
+ end
46
54
  end
@@ -3,8 +3,7 @@ require 'active_support/inflector'
3
3
 
4
4
  module Schema2type
5
5
  class SchemaConverter
6
- attr_accessor :out_text
7
- attr_reader :table_name, :convertion_table, :snake_case
6
+ attr_reader :property_lines, :table_name, :convertion_table, :snake_case
8
7
 
9
8
  TYPE_STRING = 'string'.freeze
10
9
  TYPE_NUMBER = 'number'.freeze
@@ -12,17 +11,6 @@ module Schema2type
12
11
  TYPE_DATE = 'Date'.freeze
13
12
  COLUMN_METHODS = YAML.load_file(File.expand_path(__dir__) + '/conversion_table.yml').to_a
14
13
 
15
- def initialize(table_name:, snake_case: false)
16
- @out_text = []
17
- @table_name = table_name.singularize.camelize
18
- @snake_case = snake_case
19
- end
20
-
21
- def finalize
22
- @out_text.unshift "type #{@table_name} = {"
23
- @out_text << "}\n"
24
- end
25
-
26
14
  def self.define_convert_methods(methods)
27
15
  methods.each do |m|
28
16
  define_method(m[0]) do |name, *options|
@@ -33,7 +21,17 @@ module Schema2type
33
21
 
34
22
  define_convert_methods COLUMN_METHODS
35
23
 
36
- def method_missing(*arg)
24
+ def initialize(table_name:, snake_case: false)
25
+ @property_lines = []
26
+ @table_name = table_name.singularize.camelize
27
+ @snake_case = snake_case
28
+ end
29
+
30
+ def result
31
+ ["type #{table_name} = {", property_lines, "}\n"].flatten
32
+ end
33
+
34
+ def method_missing(*)
37
35
  # To exclude unnecessary methods
38
36
  end
39
37
 
@@ -41,10 +39,10 @@ module Schema2type
41
39
 
42
40
  def push_property_line(name:, type:, options:)
43
41
  is_non_nullable = options[0] && options[0].key?(:null) && !options[0][:null]
44
- formatted_name = @snake_case ? name.underscore : name.camelcase(:lower)
42
+ formatted_name = snake_case ? name.underscore : name.camelcase(:lower)
45
43
  property_line = is_non_nullable ? "#{formatted_name}: #{type};" : "#{formatted_name}: #{type} | null;"
46
44
 
47
- @out_text << " #{property_line}"
45
+ property_lines << " #{property_line}"
48
46
  end
49
47
  end
50
48
  end
@@ -1,3 +1,3 @@
1
1
  module Schema2type
2
- VERSION = "0.1.12"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema2type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ryo