dynamic-active-model 0.5.1 → 0.5.2

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: 4f189718e66fe6718863a858f26246214c0faf3797c629798cd1fe1ad0618e72
4
- data.tar.gz: 466b853aa02d38e9fd03a46feaca892779ba05142330a4942b704e007b3faffe
3
+ metadata.gz: 852238d9a8665ebb05d038009e1ec28714bfb9b1483a55a512e5060f70f87304
4
+ data.tar.gz: c424da69e903553c01fabee7a681aa197827571aefde0c5bf941ff116f333581
5
5
  SHA512:
6
- metadata.gz: 9fbfaa0f9abde1b6a49101e48ad2f3c7f671afad6594e25b1af74dabf7bfcd9f284822b0a6dab5cd2f7ddba6d0430e007b4323284707c44d58719414d6bcd2e8
7
- data.tar.gz: e37667538308d62312f5aade7c712b872f2c6aa86a9fd0bdfa53dae535cf638b582deb19b604b58ad62055ecfb45a0d6d4d80952dabe69ec094a3d92cb4f4a3c
6
+ metadata.gz: d3c2bb4ab47309e7ac8bf171ae19048cc294e491f142a3bc7f003932193d26294c1aad119eab369520e3c1dac96d0ce9eeb8823a47d8fe018616812262a80085
7
+ data.tar.gz: bc26448991093ba1eae79ec821a1eba176ded6f09cf025ed1123cc113de1fb9a5e0f3e26504cd8b7a40ffc49b78e94db177690fbb2be54944b917fbc70fadd2b
@@ -5,7 +5,7 @@ require 'dynamic-active-model'
5
5
  require 'optparse'
6
6
  require 'yaml'
7
7
 
8
- DEFAULT_CONFIG_FILE = "#{ENV['HOME']}/.dynamic-db-explorer.yml"
8
+ DEFAULT_CONFIG_FILE = "#{ENV['HOME']}/.dynamic-db-explorer.yml".freeze
9
9
 
10
10
  options = {
11
11
  base_module: Object,
@@ -93,7 +93,7 @@ if options[:config_file]
93
93
  Object.const_set(module_name, Module.new)
94
94
  options[:base_module] = Object.const_get(module_name)
95
95
  end
96
- options[:connection_options] = config.each_with_object({}) { |(name, value), hsh| hsh[name.to_sym] = value }
96
+ options[:connection_options] = config.transform_keys(&:to_sym)
97
97
  end
98
98
 
99
99
  DYNAMIC_DATABASE = DynamicActiveModel::Explorer.explore(
@@ -73,7 +73,7 @@ module DynamicActiveModel
73
73
  if relationship_name == model.table_name.underscore
74
74
  has_many_model.table_name
75
75
  else
76
- relationship_name + '_' + has_many_model.table_name
76
+ "#{relationship_name}_#{has_many_model.table_name}"
77
77
  end
78
78
  name.underscore.pluralize.to_sym
79
79
  end
@@ -5,17 +5,17 @@ module DynamicActiveModel
5
5
  # from attribute_names method in ActiveRecord
6
6
  module DangerousAttributesPatch
7
7
  def self.included(base)
8
- if base.attribute_names
9
- columns_to_ignore = base.columns.select do |column|
10
- if column.type == :boolean
11
- base.dangerous_attribute_method?(column.name) ||
12
- base.dangerous_attribute_method?(column.name + '?')
13
- else
14
- base.dangerous_attribute_method?(column.name)
15
- end
8
+ return unless base.attribute_names
9
+
10
+ columns_to_ignore = base.columns.select do |column|
11
+ if column.type == :boolean
12
+ base.dangerous_attribute_method?(column.name) ||
13
+ base.dangerous_attribute_method?("#{column.name}?")
14
+ else
15
+ base.dangerous_attribute_method?(column.name)
16
16
  end
17
- base.ignored_columns = columns_to_ignore.map(&:name)
18
17
  end
18
+ base.ignored_columns = columns_to_ignore.map(&:name)
19
19
  end
20
20
  end
21
21
  end
@@ -8,7 +8,7 @@ module DynamicActiveModel
8
8
  :factory,
9
9
  :models
10
10
 
11
- class ModelUpdater < Struct.new(:model)
11
+ ModelUpdater = Struct.new(:model) do
12
12
  def update_model(&block)
13
13
  model.class_eval(&block)
14
14
  end
@@ -85,7 +85,7 @@ module DynamicActiveModel
85
85
  model = get_model(table_name)
86
86
  return model if model
87
87
 
88
- raise ::DynamicActiveModel::ModelNotFound.new("no model found for table #{table_name}")
88
+ raise ::DynamicActiveModel::ModelNotFound, "no model found for table #{table_name}"
89
89
  end
90
90
 
91
91
  def update_model(table_name, file = nil, &block)
@@ -95,7 +95,7 @@ module DynamicActiveModel
95
95
  model
96
96
  end
97
97
 
98
- def update_all_models(base_dir, ext='.ext.rb')
98
+ def update_all_models(base_dir, ext = '.ext.rb')
99
99
  Dir.glob("#{base_dir}/*#{ext}") do |file|
100
100
  next unless File.file?(file)
101
101
 
@@ -26,7 +26,7 @@ module DynamicActiveModel
26
26
  @base_module.const_get(class_name)
27
27
  end
28
28
 
29
- # rubocop:disable MethodLength
29
+ # rubocop:disable Metrics/MethodLength
30
30
  def base_class
31
31
  @base_class ||=
32
32
  begin
@@ -44,13 +44,13 @@ module DynamicActiveModel
44
44
  end
45
45
  end
46
46
  end
47
- # rubocop:enable MethodLength
47
+ # rubocop:enable Metrics/MethodLength
48
48
 
49
49
  def generate_class_name(table_name)
50
50
  class_name = table_name.classify
51
- return ('N' + class_name) if class_name =~ /\A\d/
51
+ return "N#{class_name}" if class_name =~ /\A\d/
52
52
 
53
- class_name
53
+ class_name
54
54
  end
55
55
  end
56
56
  end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'inheritance-helper'
4
+
5
+ module DynamicActiveModel
6
+ # DynamicActiveModel::Explorer creates models and relationships
7
+ module Setup
8
+ def self.included(base)
9
+ base.extend InheritanceHelper::Methods
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ # ClassMethods various class methods for configuring a module
14
+ module ClassMethods
15
+ def database
16
+ nil
17
+ end
18
+
19
+ def dynamic_active_model_config
20
+ {
21
+ connection_options: nil,
22
+ skip_tables: [],
23
+ relationships: {},
24
+ extensions_path: nil,
25
+ extensions_suffix: '.ext.rb'
26
+ }
27
+ end
28
+
29
+ def connection_options(options = nil)
30
+ if options.is_a?(String)
31
+ name = options
32
+ options = ActiveRecord::Base
33
+ .configurations
34
+ .configs_for(
35
+ env_name: Rails.env,
36
+ name: name
37
+ )
38
+ .configuration_hash
39
+ end
40
+
41
+ if options
42
+ config = dynamic_active_model_config
43
+ config[:connection_options] = options
44
+ redefine_class_method(:dynamic_active_model_config, config)
45
+ end
46
+
47
+ dynamic_active_model_config[:connection_options]
48
+ end
49
+
50
+ def skip_tables(tables = nil)
51
+ if tables
52
+ config = dynamic_active_model_config
53
+ config[:skip_tables] = tables
54
+ redefine_class_method(:dynamic_active_model_config, config)
55
+ end
56
+ dynamic_active_model_config[:skip_tables]
57
+ end
58
+
59
+ def skip_table(table)
60
+ config = dynamic_active_model_config
61
+ config[:skip_tables] << table
62
+ redefine_class_method(:dynamic_active_model_config, config)
63
+ end
64
+
65
+ def relationships(all_relationships = nil)
66
+ if all_relationships
67
+ config = dynamic_active_model_config
68
+ config[:relationships] = all_relationships
69
+ redefine_class_method(:dynamic_active_model_config, config)
70
+ end
71
+ dynamic_active_model_config[:relationships]
72
+ end
73
+
74
+ def foreign_key(table_name, foreign_key, relationship_name)
75
+ config = dynamic_active_model_config
76
+ current_relationships = config[:relationships]
77
+ current_relationships[table_name] ||= {}
78
+ current_relationships[table_name][foreign_key] = relationship_name
79
+ redefine_class_method(:dynamic_active_model_config, config)
80
+ end
81
+
82
+ def extensions_path(path = nil)
83
+ if path
84
+ config = dynamic_active_model_config
85
+ config[:extensions_path] = path
86
+ redefine_class_method(:dynamic_active_model_config, config)
87
+ end
88
+ dynamic_active_model_config[:extensions_path]
89
+ end
90
+
91
+ def extensions_suffix(suffix = nil)
92
+ if suffix
93
+ config = dynamic_active_model_config
94
+ config[:extensions_suffix] = suffix
95
+ redefine_class_method(:dynamic_active_model_config, config)
96
+ end
97
+ dynamic_active_model_config[:extensions_suffix]
98
+ end
99
+
100
+ def create_models!
101
+ redefine_class_method(
102
+ :database,
103
+ DynamicActiveModel::Explorer.explore(
104
+ self,
105
+ connection_options,
106
+ skip_tables,
107
+ relationships
108
+ )
109
+ )
110
+ database.update_all_models(extensions_path, extensions_suffix) if extensions_path
111
+ database
112
+ end
113
+ end
114
+ end
115
+ end
@@ -9,6 +9,7 @@ module DynamicActiveModel
9
9
  autoload :ForeignKey, 'dynamic-active-model/foreign_key'
10
10
  autoload :Associations, 'dynamic-active-model/associations'
11
11
  autoload :TemplateClassFile, 'dynamic-active-model/template_class_file'
12
+ autoload :Setup, 'dynamic-active-model/setup'
12
13
 
13
- class ModelNotFound < Exception; end
14
+ class ModelNotFound < StandardError; end
14
15
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic-active-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-10-09 00:00:00.000000000 Z
10
+ date: 2025-04-28 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activerecord
@@ -24,6 +23,20 @@ dependencies:
24
23
  - - ">="
25
24
  - !ruby/object:Gem::Version
26
25
  version: '0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: inheritance-helper
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
27
40
  description: Dynamically create ActiveRecord models for tables
28
41
  email: dougyouch@gmail.com
29
42
  executables:
@@ -39,12 +52,12 @@ files:
39
52
  - lib/dynamic-active-model/explorer.rb
40
53
  - lib/dynamic-active-model/factory.rb
41
54
  - lib/dynamic-active-model/foreign_key.rb
55
+ - lib/dynamic-active-model/setup.rb
42
56
  - lib/dynamic-active-model/template_class_file.rb
43
57
  homepage: https://github.com/dougyouch/dynamic-active-model
44
58
  licenses:
45
59
  - MIT
46
60
  metadata: {}
47
- post_install_message:
48
61
  rdoc_options: []
49
62
  require_paths:
50
63
  - lib
@@ -59,8 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
72
  - !ruby/object:Gem::Version
60
73
  version: '0'
61
74
  requirements: []
62
- rubygems_version: 3.5.3
63
- signing_key:
75
+ rubygems_version: 3.6.2
64
76
  specification_version: 4
65
77
  summary: Dynamic ActiveRecord Models
66
78
  test_files: []