dynamic-active-model 0.4.7 → 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: 887a1be666cb15d06e0f517caec88c00513145e96972356ecaae580f5490598a
4
- data.tar.gz: e5e842896b857a0dcdca0c3f03f57557cb5535f59be6dd3d5187eda291e3f895
3
+ metadata.gz: 852238d9a8665ebb05d038009e1ec28714bfb9b1483a55a512e5060f70f87304
4
+ data.tar.gz: c424da69e903553c01fabee7a681aa197827571aefde0c5bf941ff116f333581
5
5
  SHA512:
6
- metadata.gz: 2dc58c66d022476449054c486ceff7246367884e22a8752e73faf9cacec169a13a6a8af015aa9168f10934789be0da86920c781185456b9802762a3e9f274fac
7
- data.tar.gz: acf7207e5939a47d99e2f4e4c6375347da7353b29ebfbf34d6356bc71e1882445239b333a58f621de89117f46ce29fc260cf22d9b7b7e658e7ab17a92cf3cac1
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,6 +8,12 @@ module DynamicActiveModel
8
8
  :factory,
9
9
  :models
10
10
 
11
+ ModelUpdater = Struct.new(:model) do
12
+ def update_model(&block)
13
+ model.class_eval(&block)
14
+ end
15
+ end
16
+
11
17
  def initialize(base_module, connection_options, base_class_name = nil)
12
18
  @factory = Factory.new(base_module, connection_options, base_class_name)
13
19
  @table_class_names = {}
@@ -70,6 +76,34 @@ module DynamicActiveModel
70
76
  end
71
77
  alias disable_sti! disable_standard_table_inheritance!
72
78
 
79
+ def get_model(table_name)
80
+ table_name = table_name.to_s
81
+ models.detect { |model| model.table_name == table_name }
82
+ end
83
+
84
+ def get_model!(table_name)
85
+ model = get_model(table_name)
86
+ return model if model
87
+
88
+ raise ::DynamicActiveModel::ModelNotFound, "no model found for table #{table_name}"
89
+ end
90
+
91
+ def update_model(table_name, file = nil, &block)
92
+ model = get_model!(table_name)
93
+ ModelUpdater.new(model).instance_eval(File.read(file)) if file
94
+ model.class_eval(&block) if block
95
+ model
96
+ end
97
+
98
+ def update_all_models(base_dir, ext = '.ext.rb')
99
+ Dir.glob("#{base_dir}/*#{ext}") do |file|
100
+ next unless File.file?(file)
101
+
102
+ table_name = File.basename(file).split('.', 2).first
103
+ update_model(table_name, file)
104
+ end
105
+ end
106
+
73
107
  private
74
108
 
75
109
  def skip_table?(table_name)
@@ -23,11 +23,10 @@ module DynamicActiveModel
23
23
  include DynamicActiveModel::DangerousAttributesPatch
24
24
  end
25
25
  @base_module.const_set(class_name, kls)
26
- require_extension(class_name)
27
26
  @base_module.const_get(class_name)
28
27
  end
29
28
 
30
- # rubocop:disable MethodLength
29
+ # rubocop:disable Metrics/MethodLength
31
30
  def base_class
32
31
  @base_class ||=
33
32
  begin
@@ -45,20 +44,13 @@ module DynamicActiveModel
45
44
  end
46
45
  end
47
46
  end
48
- # rubocop:enable MethodLength
47
+ # rubocop:enable Metrics/MethodLength
49
48
 
50
49
  def generate_class_name(table_name)
51
50
  class_name = table_name.classify
52
- return ('N' + class_name) if class_name =~ /\A\d/
51
+ return "N#{class_name}" if class_name =~ /\A\d/
53
52
 
54
- class_name
55
- end
56
-
57
- def require_extension(class_name)
58
- file = File.expand_path(DynamicActiveModel.base_models_path + '/' + class_name.underscore + '.rb')
59
- return unless File.exist?(file)
60
-
61
- require file
53
+ class_name
62
54
  end
63
55
  end
64
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,12 +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
- def self.base_models_path
14
- @base_models_path || 'app/models'
15
- end
16
-
17
- def self.base_models_path=(path)
18
- @base_models_path = path
19
- end
14
+ class ModelNotFound < StandardError; end
20
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.4.7
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: 2023-03-04 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.3.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: []