infer_model 0.1.1 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +30 -29
- data/lib/infer_model/from/csv.rb +9 -1
- data/lib/infer_model/parsers/date_time.rb +9 -0
- data/lib/infer_model/to/migration.rb +16 -4
- data/lib/infer_model/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 04ed2000c2f891a8afbecbeb147ea60b99f2d75c3e98894037c9858311d38f2c
|
4
|
+
data.tar.gz: fe283d10e13d59f2579b87639c7707a5e45470bf825b2b71410ff55e1e78bfd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b88b7f67b42df71dafa09e5b3748a4dca6d3ea7be1c1d36c7a3b9eb834ac906648df33969bbfd8ac426f2ce830206684dd918e72114337f29ef9c1a2f6367b1d
|
7
|
+
data.tar.gz: c75104eecb2ff301cc46c760dad9c2d4985e93e11546ad0caad799f999bd67788507febadaa72367ee44fd25d4f9eab2b1e07ec8c89418f104cc8885a21f0eaa
|
data/.rubocop.yml
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
3.1
|
1
|
+
3.2.1
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
infer_model (0.1.
|
4
|
+
infer_model (0.1.4)
|
5
5
|
activesupport (~> 7.0)
|
6
6
|
dry-initializer (~> 3.0)
|
7
7
|
zeitwerk (~> 2.6)
|
@@ -9,68 +9,69 @@ PATH
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
activesupport (7.0.4)
|
12
|
+
activesupport (7.0.4.2)
|
13
13
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
14
|
i18n (>= 1.6, < 2)
|
15
15
|
minitest (>= 5.1)
|
16
16
|
tzinfo (~> 2.0)
|
17
17
|
ast (2.4.2)
|
18
18
|
coderay (1.1.3)
|
19
|
-
concurrent-ruby (1.
|
19
|
+
concurrent-ruby (1.2.0)
|
20
20
|
diff-lcs (1.5.0)
|
21
21
|
dotenv (2.8.1)
|
22
22
|
dry-initializer (3.1.1)
|
23
23
|
i18n (1.12.0)
|
24
24
|
concurrent-ruby (~> 1.0)
|
25
|
-
json (2.6.
|
25
|
+
json (2.6.3)
|
26
26
|
method_source (1.0.0)
|
27
|
-
minitest (5.
|
27
|
+
minitest (5.17.0)
|
28
28
|
parallel (1.22.1)
|
29
|
-
parser (3.
|
29
|
+
parser (3.2.1.0)
|
30
30
|
ast (~> 2.4.1)
|
31
|
-
pry (0.14.
|
31
|
+
pry (0.14.2)
|
32
32
|
coderay (~> 1.1)
|
33
33
|
method_source (~> 1.0)
|
34
34
|
rainbow (3.1.1)
|
35
35
|
rake (13.0.6)
|
36
|
-
regexp_parser (2.
|
36
|
+
regexp_parser (2.7.0)
|
37
37
|
rexml (3.2.5)
|
38
|
-
rspec (3.
|
39
|
-
rspec-core (~> 3.
|
40
|
-
rspec-expectations (~> 3.
|
41
|
-
rspec-mocks (~> 3.
|
42
|
-
rspec-core (3.
|
43
|
-
rspec-support (~> 3.
|
44
|
-
rspec-expectations (3.
|
38
|
+
rspec (3.12.0)
|
39
|
+
rspec-core (~> 3.12.0)
|
40
|
+
rspec-expectations (~> 3.12.0)
|
41
|
+
rspec-mocks (~> 3.12.0)
|
42
|
+
rspec-core (3.12.1)
|
43
|
+
rspec-support (~> 3.12.0)
|
44
|
+
rspec-expectations (3.12.2)
|
45
45
|
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
-
rspec-support (~> 3.
|
47
|
-
rspec-mocks (3.
|
46
|
+
rspec-support (~> 3.12.0)
|
47
|
+
rspec-mocks (3.12.3)
|
48
48
|
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
-
rspec-support (~> 3.
|
50
|
-
rspec-support (3.
|
51
|
-
rubocop (1.
|
49
|
+
rspec-support (~> 3.12.0)
|
50
|
+
rspec-support (3.12.0)
|
51
|
+
rubocop (1.45.1)
|
52
52
|
json (~> 2.3)
|
53
53
|
parallel (~> 1.10)
|
54
|
-
parser (>= 3.
|
54
|
+
parser (>= 3.2.0.0)
|
55
55
|
rainbow (>= 2.2.2, < 4.0)
|
56
56
|
regexp_parser (>= 1.8, < 3.0)
|
57
57
|
rexml (>= 3.2.5, < 4.0)
|
58
|
-
rubocop-ast (>= 1.
|
58
|
+
rubocop-ast (>= 1.24.1, < 2.0)
|
59
59
|
ruby-progressbar (~> 1.7)
|
60
|
-
unicode-display_width (>=
|
61
|
-
rubocop-ast (1.
|
62
|
-
parser (>= 3.
|
63
|
-
rubocop-performance (1.
|
60
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
61
|
+
rubocop-ast (1.26.0)
|
62
|
+
parser (>= 3.2.1.0)
|
63
|
+
rubocop-performance (1.16.0)
|
64
64
|
rubocop (>= 1.7.0, < 2.0)
|
65
65
|
rubocop-ast (>= 0.4.0)
|
66
66
|
ruby-progressbar (1.11.0)
|
67
|
-
tzinfo (2.0.
|
67
|
+
tzinfo (2.0.6)
|
68
68
|
concurrent-ruby (~> 1.0)
|
69
|
-
unicode-display_width (2.2
|
70
|
-
zeitwerk (2.6.
|
69
|
+
unicode-display_width (2.4.2)
|
70
|
+
zeitwerk (2.6.7)
|
71
71
|
|
72
72
|
PLATFORMS
|
73
73
|
arm64-darwin-21
|
74
|
+
arm64-darwin-22
|
74
75
|
x86_64-linux
|
75
76
|
|
76
77
|
DEPENDENCIES
|
data/lib/infer_model/from/csv.rb
CHANGED
@@ -13,6 +13,7 @@ module InferModel::From
|
|
13
13
|
param :filename
|
14
14
|
option :available_types, default: -> { ::InferModel::ValueTypeGuesser::RESULT_TYPES }
|
15
15
|
option :multi, default: -> { false }
|
16
|
+
option :rename_attributes, optional: true
|
16
17
|
option :csv_options, default: -> { {} }
|
17
18
|
|
18
19
|
def call
|
@@ -26,12 +27,19 @@ module InferModel::From
|
|
26
27
|
def attributes
|
27
28
|
csv.by_col!.to_h do |header, contents|
|
28
29
|
[
|
29
|
-
header
|
30
|
+
transform_header(header),
|
30
31
|
::InferModel::CommonTypeGuesser.call(contents, available_types:),
|
31
32
|
]
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
36
|
+
def transform_header(header)
|
37
|
+
header = header.downcase.to_sym
|
38
|
+
return header unless rename_attributes
|
39
|
+
|
40
|
+
rename_attributes.to_proc.call(header) || header
|
41
|
+
end
|
42
|
+
|
35
43
|
def csv = ::CSV.parse(file_content, **csv_options_with_defaults)
|
36
44
|
|
37
45
|
def file_content = File.read(filename)
|
@@ -22,11 +22,20 @@ module InferModel
|
|
22
22
|
|
23
23
|
param :value
|
24
24
|
option :allow_blank, default: -> { true }
|
25
|
+
option :time_zone_offset, default: -> { "+00:00" }
|
25
26
|
|
26
27
|
def call
|
27
28
|
raise Parsers::Error, "value was blank which is not allowed" if value.nil? && !allow_blank
|
28
29
|
return if value.nil? || value.empty?
|
29
30
|
|
31
|
+
datetime_without_zone = parsed_datetime.iso8601[..-7]
|
32
|
+
datetime_with_custom_zone = datetime_without_zone + time_zone_offset
|
33
|
+
DateTime.parse(datetime_with_custom_zone)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def parsed_datetime
|
30
39
|
ACCEPTABLE_DATETIME_FORMATS.each do |format|
|
31
40
|
return DateTime.strptime(value, format)
|
32
41
|
rescue Date::Error
|
@@ -10,9 +10,11 @@ module InferModel::To
|
|
10
10
|
TIMESTAMP_FORMAT = "%Y%m%d%H%M%S"
|
11
11
|
|
12
12
|
param :model
|
13
|
-
option :
|
14
|
-
option :
|
13
|
+
option :attributes_map, optional: true
|
14
|
+
option :filter_on_attribute_mapping, default: -> { true }
|
15
15
|
option :rails_version, default: -> { "7.0" }
|
16
|
+
option :table_name, optional: true
|
17
|
+
option :target_dir, default: -> { "db/migrate" }
|
16
18
|
|
17
19
|
def call
|
18
20
|
FileUtils.mkdir_p(target_dir)
|
@@ -30,6 +32,16 @@ module InferModel::To
|
|
30
32
|
table_name || model.source_name.pluralize
|
31
33
|
end
|
32
34
|
|
35
|
+
def given_or_mapped_attributes
|
36
|
+
return model.attributes unless attributes_map
|
37
|
+
|
38
|
+
if filter_on_attribute_mapping
|
39
|
+
attributes_map.to_h { |old_key, new_key| [new_key, model.attributes[old_key]] }
|
40
|
+
else
|
41
|
+
model.attributes.transform_keys { |key| attributes_map.fetch(key, key) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
33
45
|
def migration_content
|
34
46
|
<<~RUBY
|
35
47
|
# frozen_string_literal: true
|
@@ -49,13 +61,13 @@ module InferModel::To
|
|
49
61
|
COLUMN_DDL_LINES_WITH_INDENTATION_JOINER = "\n#{" " * 3}".freeze
|
50
62
|
|
51
63
|
def column_ddl_lines
|
52
|
-
column_definitions =
|
64
|
+
column_definitions = given_or_mapped_attributes.map do |key, common_type|
|
53
65
|
attribute_and_name = %(t.#{common_type.detected_type} "#{key}")
|
54
66
|
non_null_constraint = common_type.non_null_constraint_possible ? "null: false" : nil
|
55
67
|
|
56
68
|
[attribute_and_name, non_null_constraint].compact.join(", ")
|
57
69
|
end
|
58
|
-
index_definitions =
|
70
|
+
index_definitions = given_or_mapped_attributes.filter_map do |key, common_type|
|
59
71
|
next unless common_type.unique_constraint_possible
|
60
72
|
|
61
73
|
%(t.index ["#{key}"], unique: true)
|
data/lib/infer_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: infer_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jay Schneider
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -201,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
201
201
|
- !ruby/object:Gem::Version
|
202
202
|
version: '0'
|
203
203
|
requirements: []
|
204
|
-
rubygems_version: 3.
|
204
|
+
rubygems_version: 3.4.6
|
205
205
|
signing_key:
|
206
206
|
specification_version: 4
|
207
207
|
summary: Infer data types from external sources to create a rails model for example
|