schema2type 0.2.2 → 0.2.3
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 +4 -4
- data/exe/schema2type +1 -1
- data/lib/schema2type/cli.rb +4 -4
- data/lib/schema2type/covert_service.rb +11 -5
- data/lib/schema2type/schema_converter.rb +10 -8
- data/lib/schema2type/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 93dcd338891b9f159af21fd6c79db61c4b6b3fa7ee3b3aeee1b52a35ffef9380
|
4
|
+
data.tar.gz: 58831c346ebfc9fcd9ba867c34f8c83b2a1a9c2a57b09327bdbc504e203eb18e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d26fd7d40936f4c2720b06fe325583fdd3d6c32995c1176b6edd0498c1c4a8078d6d83a873544592f0be3454becd40cbb8728104ea87a486be25e4447fb1904
|
7
|
+
data.tar.gz: 383af1848ec7e2169ce2d744938790e26b8eec8745ea15af526e448281fcb0041457a9f56a6e0e8d7cdd84c481566865f454d847a359ed3b3c2164c297068731
|
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'],
|
8
|
+
Schema2type.execute(input_file: params['s'], out_file: params['o'], name_space: params['n'], is_snake_case: params['snake'])
|
data/lib/schema2type/cli.rb
CHANGED
@@ -3,9 +3,9 @@ module Schema2type
|
|
3
3
|
DEFAULT_SCHEMA_PATH = "./db/schema.rb".freeze
|
4
4
|
DEFAULT_NAME_SPACE = "schema".freeze
|
5
5
|
|
6
|
-
def self.execute(input_file:, out_file:, name_space:,
|
6
|
+
def self.execute(input_file:, out_file:, name_space:, is_snake_case:)
|
7
7
|
|
8
|
-
|
8
|
+
resultHash = eval(File.read(input_file || DEFAULT_SCHEMA_PATH), CovertService.new(is_snake_case).get_binding)
|
9
9
|
|
10
10
|
File.open(out_file, "w") do |f|
|
11
11
|
f.puts <<~EOS
|
@@ -13,11 +13,11 @@ module Schema2type
|
|
13
13
|
|
14
14
|
/**
|
15
15
|
* auto-generated file
|
16
|
-
* schema version: #{
|
16
|
+
* schema version: #{resultHash[:version]}
|
17
17
|
* This file was automatically generated by schema2type
|
18
18
|
*/
|
19
19
|
declare namespace #{name_space || DEFAULT_NAME_SPACE} {
|
20
|
-
#{
|
20
|
+
#{resultHash[:lines]}
|
21
21
|
}
|
22
22
|
EOS
|
23
23
|
end
|
@@ -1,28 +1,32 @@
|
|
1
1
|
module Schema2type
|
2
2
|
class CovertService
|
3
|
-
def initialize(
|
3
|
+
def initialize(is_snake_case)
|
4
4
|
@converted_types = []
|
5
|
-
@
|
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
|
-
|
13
|
-
|
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.
|
16
|
+
@converted_types.concat converter.converted_type_lines
|
16
17
|
end
|
17
18
|
|
18
19
|
def method_missing(*)
|
19
20
|
# To exclude unnecessary methods
|
21
|
+
# TODO: add error handling
|
20
22
|
end
|
21
23
|
|
22
24
|
def self.method_missing(*)
|
23
25
|
# To exclude unnecessary methods
|
26
|
+
# TODO: add error handling
|
24
27
|
end
|
25
28
|
|
29
|
+
# mock module and method for shcema.rb
|
26
30
|
module ActiveRecord
|
27
31
|
module Schema
|
28
32
|
def self.define(*arg)
|
@@ -35,5 +39,7 @@ module Schema2type
|
|
35
39
|
end
|
36
40
|
end
|
37
41
|
end
|
42
|
+
|
43
|
+
alias create_table convert_schema_to_type
|
38
44
|
end
|
39
45
|
end
|
@@ -3,43 +3,45 @@ require 'active_support/inflector'
|
|
3
3
|
|
4
4
|
module Schema2type
|
5
5
|
class SchemaConverter
|
6
|
-
attr_reader :property_lines, :table_name, :
|
6
|
+
attr_reader :property_lines, :table_name, :is_snake_case
|
7
7
|
|
8
8
|
TYPE_STRING = 'string'.freeze
|
9
9
|
TYPE_NUMBER = 'number'.freeze
|
10
10
|
TYPE_BOOLEAN = 'boolean'.freeze
|
11
11
|
TYPE_DATE = 'Date'.freeze
|
12
12
|
COLUMN_METHODS = YAML.load_file(File.expand_path(__dir__) + '/conversion_table.yml').to_a
|
13
|
+
ID_PROPERTY_LINE_TEXT = " id: number;".freeze
|
13
14
|
|
14
15
|
def self.define_convert_methods(methods)
|
15
16
|
methods.each do |m|
|
16
17
|
define_method(m[0]) do |name, *options|
|
17
|
-
|
18
|
+
convert_property_line_and_push name: name, type: m[1], options: options
|
18
19
|
end
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
23
|
define_convert_methods COLUMN_METHODS
|
23
24
|
|
24
|
-
def initialize(table_name:,
|
25
|
+
def initialize(table_name:, is_snake_case: false)
|
25
26
|
@property_lines = []
|
26
27
|
@table_name = table_name.singularize.camelize
|
27
|
-
@
|
28
|
+
@is_snake_case = is_snake_case
|
28
29
|
end
|
29
30
|
|
30
|
-
def
|
31
|
-
["type #{table_name} = {", property_lines, "}\n"].flatten
|
31
|
+
def converted_type_lines
|
32
|
+
["type #{table_name} = {", ID_PROPERTY_LINE_TEXT, property_lines, "}\n"].flatten
|
32
33
|
end
|
33
34
|
|
34
35
|
def method_missing(*)
|
35
36
|
# To exclude unnecessary methods
|
37
|
+
# TODO: add error handling
|
36
38
|
end
|
37
39
|
|
38
40
|
private
|
39
41
|
|
40
|
-
def
|
42
|
+
def convert_property_line_and_push(name:, type:, options:)
|
41
43
|
is_non_nullable = options[0] && options[0].key?(:null) && !options[0][:null]
|
42
|
-
formatted_name =
|
44
|
+
formatted_name = is_snake_case ? name.underscore : name.camelcase(:lower)
|
43
45
|
property_line = is_non_nullable ? "#{formatted_name}: #{type};" : "#{formatted_name}: #{type} | null;"
|
44
46
|
|
45
47
|
property_lines << " #{property_line}"
|
data/lib/schema2type/version.rb
CHANGED
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.
|
4
|
+
version: 0.2.3
|
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-
|
11
|
+
date: 2019-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|