standalone_activerecord_boot_loader 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4d8f08d56bc81a54009dceeb08cb557f028b0fb2d2114429d49e531bae30f7da
4
+ data.tar.gz: 2c5227c63b26e9d7fc743ad6049bbe1499ec78c1d1321c1347d1e04b1d70cee6
5
+ SHA512:
6
+ metadata.gz: c5d433dea59d6197dd8ddd9f91e1d4012ed5d6221c1a3a499354d19132eeee94eb516b671f92935f13d168e13089096cfae400714aa0e0de07ac316647c29aa9
7
+ data.tar.gz: c44157dfffbe29e425fda0020ab9e0f642e830318bf5922870480c5d343beeada8a24b9f4109e940a2765b50f23424af6622d081b1372614df50ee9dc10ec9a1
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in standalone_activerecord_boot_loader.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Takahiro Ooishi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # StandaloneActiverecordBootLoader
2
+
3
+ ActiveRecordを単体で動かす時に使う起動処理
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'standalone_activerecord_boot_loader'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install standalone_activerecord_boot_loader
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ instance = StandaloneActiverecordBootLoader::Instance.new(
25
+ Pathname.new('path-to-dummy-app-dir')
26
+ )
27
+ instance.execute
28
+ ```
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/taka0125/standalone_activerecord_boot_loader.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "standalone_activerecord_boot_loader"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StandaloneActiverecordBootLoader
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'standalone_activerecord_boot_loader/version'
4
+ require 'zeitwerk'
5
+ require 'active_record'
6
+ require 'active_record/tasks/database_tasks'
7
+ require 'active_support/time'
8
+
9
+ module StandaloneActiverecordBootLoader
10
+ class Error < StandardError; end
11
+
12
+ class Instance
13
+ def initialize(app_root, create_database: true, env: 'development', timezone: 'Tokyo')
14
+ @app_root = app_root
15
+ @create_database = create_database
16
+ @env = env
17
+ @timezone = timezone
18
+ end
19
+
20
+ def execute
21
+ setup_zeitwerk
22
+ setup_active_record
23
+ end
24
+
25
+ private
26
+
27
+ def setup_zeitwerk
28
+ loader = Zeitwerk::Loader.new
29
+ loader.push_dir(@app_root.join('app/models'))
30
+ loader.setup
31
+ end
32
+
33
+ def setup_active_record
34
+ create_database if @create_database
35
+ Time.zone = @timezone
36
+ ActiveRecord::Base.establish_connection(database_config[@env])
37
+ ActiveRecord::Base.time_zone_aware_attributes = true
38
+ end
39
+
40
+ def create_database
41
+ conn_spec = database_config[@env]
42
+
43
+ # ActiveRecord 6系以降
44
+ if defined?(ActiveRecord::DatabaseConfigurations::HashConfig)
45
+ conn_spec = ActiveRecord::DatabaseConfigurations::HashConfig.new(@env, 'primary', conn_spec)
46
+ end
47
+
48
+ tasks = ActiveRecord::Tasks::MySQLDatabaseTasks.new(conn_spec)
49
+ begin
50
+ tasks.create
51
+ rescue => e
52
+ # ActiveRecord 5系
53
+ if defined?(ActiveRecord::Tasks::DatabaseAlreadyExists)
54
+ return if e.instance_of?(ActiveRecord::Tasks::DatabaseAlreadyExists)
55
+ end
56
+
57
+ # ActiveRecord 6系以降
58
+ if defined?(ActiveRecord::DatabaseAlreadyExists)
59
+ return if e.instance_of?(ActiveRecord::DatabaseAlreadyExists)
60
+ end
61
+
62
+ raise e
63
+ end
64
+ end
65
+
66
+ def database_yml_path
67
+ @app_root.join('config/database.yml')
68
+ end
69
+
70
+ def database_config
71
+ @database_config ||= YAML::load(ERB.new(File.read(database_yml_path)).result)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/standalone_activerecord_boot_loader/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "standalone_activerecord_boot_loader"
7
+ spec.version = StandaloneActiverecordBootLoader::VERSION
8
+ spec.authors = ["Takahiro Ooishi"]
9
+ spec.email = ["taka0125@gmail.com"]
10
+
11
+ spec.summary = "ActiveRecordを単体で動かす時に使う起動処理"
12
+ spec.description = "ActiveRecordを単体で動かす時に使う起動処理"
13
+ spec.homepage = "https://github.com/taka0125/standalone_activerecord_boot_loader"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "zeitwerk"
31
+ spec.add_dependency "activerecord"
32
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: standalone_activerecord_boot_loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takahiro Ooishi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: zeitwerk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ActiveRecordを単体で動かす時に使う起動処理
42
+ email:
43
+ - taka0125@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/standalone_activerecord_boot_loader.rb
56
+ - lib/standalone_activerecord_boot_loader/version.rb
57
+ - standalone_activerecord_boot_loader.gemspec
58
+ homepage: https://github.com/taka0125/standalone_activerecord_boot_loader
59
+ licenses: []
60
+ metadata:
61
+ homepage_uri: https://github.com/taka0125/standalone_activerecord_boot_loader
62
+ source_code_uri: https://github.com/taka0125/standalone_activerecord_boot_loader
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 2.6.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.2.22
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: ActiveRecordを単体で動かす時に使う起動処理
82
+ test_files: []