infer_model 0.1.1 → 0.1.2

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: 11b33f155e0db1415140efd8c4694aad4499ffdbd2c91cf239bba1d527fd2d40
4
- data.tar.gz: 22514951be567f30c8743aa4c53749cb2441411ce77fd6017172b914d2d40629
3
+ metadata.gz: 89e0007e60d9ffa17f7ceb8f12e157be7c8d3570e3edaf5acd7ae9c3d397478e
4
+ data.tar.gz: c622cb756b83391f44328e0a85ca2b3cd46df5e7f171757deb2aca36f3f7cd3e
5
5
  SHA512:
6
- metadata.gz: 57e31a6031eb46c9dee5e121e152a678fe60a464ee478901dd025556bbfcd2f5fba5ad8ca36142bfdb675aaca7c067828df185a34c9b4b8c7537b09bcde675a4
7
- data.tar.gz: ef70c7981755957dd0ad8e0494793844b119fd323fc202fbb70019c227dc3f5c196121bb5a27b5016eeb672e0f62eddb186a7f785606af924245cd2ec73752cb
6
+ metadata.gz: f1a0328a1fa5fb4b3eaf22c7e7fe19e206041f847859e8b7291ebd1b4f1ec520b1e54fd6644f62733e619f6e7990b6571f189951f45266561dd9a365e1af4e43
7
+ data.tar.gz: 508256b0bb1a0e40f0b5ae7881c141dcea149fa3b74817d37542a77df8496f8cd485938afb9ea2ac085f68ee273b82b038350be0e4384fb8ed0e4edc1d64c234
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.2] - 2022-09-15
4
+
5
+ - feat: Allow renaming attributes in to/migration & from/csv adapters
6
+
3
7
  ## [0.1.1] - 2022-09-13
4
8
 
5
9
  - Bump dependencies
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- infer_model (0.1.1)
4
+ infer_model (0.1.2)
5
5
  activesupport (~> 7.0)
6
6
  dry-initializer (~> 3.0)
7
7
  zeitwerk (~> 2.6)
@@ -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.downcase.to_sym,
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)
@@ -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 :target_dir, default: -> { "db/migrate" }
14
- option :table_name, optional: true
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 = model.attributes.map do |key, common_type|
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 = model.attributes.filter_map do |key, common_type|
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module InferModel
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jay Schneider
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-13 00:00:00.000000000 Z
11
+ date: 2022-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport