ordered_initializers 0.3.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d425bf8b9398491a77bca28f215e83f90ccc061152628ffa966a9bdbc1c52228
4
+ data.tar.gz: 21c7298c590081853528ed8324235461347f65737a4cc4038bfd1aa1183aeeed
5
+ SHA512:
6
+ metadata.gz: e4e1e4142f7ebf7f2dfbec1020c2d4cc23c7d8368fbcbc064f32407d5b3bca65b8597b480b17336d41e13da71a8f17c517c8ccef10f01edb86482bd331dd569e
7
+ data.tar.gz: 4263dfc8339ff8938d57f40418072f907a661ff990f0ffd966a621e48aa50a15508261887715710a964c114e8477c13f908a6c10eb071a3bb7a281c8be6f4905
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Brandon Keepers
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ According to [Rails doc](https://guides.rubyonrails.org/configuring.html#using-initializer-files)
2
+ > After loading the framework and any gems in your application, Rails turns to loading initializers. An initializer is any Ruby file stored under config/initializers in your Rails application.
3
+ The files in config/initializers are _sorted_ and _loaded_ _one by one_ as part of the load_config_initializers initializer.
4
+ If an initializer has code that relies on code in another initializer, you can combine them into a single initializer instead. This makes the dependencies more explicit, and can help surface new concepts within your application. Rails also supports _numbering of initializer file names_, but this can lead to file name churn.
5
+
6
+ Ordered Initiliazers simply allows you to choose the order in which you want your initializers to be loaded, independently of their name.
7
+
8
+ # Installation
9
+
10
+ Add this line to the top of your Rails application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'ordered_initializers'
14
+ ```
15
+
16
+ And then install the gem:
17
+
18
+ ```shell
19
+ $ bundle install
20
+ ```
21
+
22
+ You can set your initializers order in the `config/initializers.yml` file. Because there is no possibility to prevent Rails from loading files in `config/initializers`, you need to store your initializers files in the `config/ordered_initializers` folder.
23
+
24
+ Ordered Initiliazers provides a rake task that will automatically moves the files from `config/initializers` to`config/ordered_initializers`, generates the `config/initializers.yml` file and leaves a small reminder in `config/initializers`.
25
+
26
+ ```shell
27
+ $ rake ordered_initializers:install
28
+ ```
29
+
30
+ You can reorder the initializers path as you see fit.
31
+
32
+ You can still add files to `config/initializers`. They will be loaded alphabetically after the ones in `config/ordered_initializers`.
33
+
34
+ Running `rake ordered_initializers:install` again will simply append the new files from `config/initializers` at the end of `config/initializers.yml`.
@@ -0,0 +1,21 @@
1
+ module OrderedInitializers
2
+ class Parser
3
+ class << self
4
+ def path
5
+ "config/initializers.yml"
6
+ end
7
+ end
8
+
9
+ def initializer_files
10
+ YAML.safe_load(yml_file, aliases: true)
11
+ rescue ArgumentError
12
+ YAML.safe_load(yml_file)
13
+ end
14
+
15
+ private
16
+
17
+ def yml_file
18
+ File.read(Rails.root.join(self.class.path))
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ require "rails"
2
+ require "ordered_initializers/parser"
3
+ require "ordered_initializers"
4
+
5
+ module OrderedInitializers
6
+ class Railtie < ::Rails::Railtie
7
+ rake_tasks do
8
+ path = File.expand_path(__dir__)
9
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
10
+ end
11
+
12
+ def load_initializer_file
13
+ OrderedInitializers.initializer_files = parsed
14
+ end
15
+
16
+ def parsed
17
+ Parser.new.initializer_files
18
+ end
19
+
20
+ private
21
+
22
+ config.before_initialize do
23
+ load_initializer_file
24
+
25
+ OrderedInitializers.go
26
+ rescue Errno::ENOENT
27
+ ::Kernel.warn("Missing file #{Parser.path}! Please run `rake ordered_initializers:install` to generate the file. Skipping ordered_initializers for the moment")
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,89 @@
1
+ namespace :ordered_initializers do
2
+ desc "Moves files from config/initializers to config/ordered_initializers and generates config/initializers.yml"
3
+ task :install do
4
+ def from_dir
5
+ Rails.root.join("config/initializers")
6
+ end
7
+
8
+ def to_dir
9
+ Rails.root.join("config/ordered_initializers").tap do |path|
10
+ Dir.mkdir(path) unless Dir.exist?(path)
11
+ end
12
+ end
13
+
14
+ def rails_initializers_yml_path
15
+ Rails.root.join("config/initializers.yml")
16
+ end
17
+
18
+ def existing_initializer_files_path
19
+ YAML.load_file(Rails.root.join(rails_initializers_yml_path)) || []
20
+ rescue
21
+ []
22
+ end
23
+
24
+ def all_initializer_files_path
25
+ Dir[to_dir + "*"].map { |path| to_relative_path(path) }
26
+ end
27
+
28
+ def to_relative_path(path)
29
+ path.sub(/#{Rails.root}\//, "")
30
+ end
31
+
32
+ def move_initializers!
33
+ ::Kernel.warn("Moving all existing initializers from #{to_relative_path(from_dir)} to #{to_relative_path(to_dir)}")
34
+
35
+ Dir[from_dir + "*"].each do |old_dest|
36
+ next if /0_placeholder.rb/.match?(old_dest.to_s)
37
+ new_dest = old_dest.to_s.gsub(from_dir.to_s, to_dir.to_s)
38
+ FileUtils.mv(old_dest, new_dest)
39
+ end
40
+ end
41
+
42
+ # Keep the already existing initializers path and append the new ones at the end
43
+ def update_initializers_yml!
44
+ if File.exist?(rails_initializers_yml_path)
45
+ ::Kernel.warn("Updating #{to_relative_path(rails_initializers_yml_path)}")
46
+ else
47
+ ::Kernel.warn("Creating #{to_relative_path(rails_initializers_yml_path)}")
48
+ end
49
+
50
+ new_initializer_files_path = all_initializer_files_path - existing_initializer_files_path
51
+ initializer_files_path = existing_initializer_files_path + new_initializer_files_path
52
+
53
+ File.open(rails_initializers_yml_path, "w") do |f|
54
+ f.puts "# this file have been generated using `rake ordered_initializers:install`"
55
+ f.puts "# you can reorder the initializers path as you see fit"
56
+ f.write initializer_files_path.to_yaml
57
+ end
58
+
59
+ ::Kernel.warn("The initializers will be load in the following order:")
60
+ initializer_files_path.each do |path|
61
+ ::Kernel.warn("- #{to_relative_path(path)}")
62
+ end
63
+ ::Kernel.warn("You can reorder the initializers path as you see fit.")
64
+ end
65
+
66
+ def add_placeholder
67
+ ::Kernel.warn("Generating config/initializers/0_placeholder.rb")
68
+
69
+ File.open(from_dir.join("0_placeholder.rb"), "w") do |f|
70
+ f.puts "# All the files from this folder were moved to config/ordered_initializers folder using `rake ordered_initializers:install`"
71
+ f.puts "# You can still add files to this folder. They will be loaded alphabetically after the ones in config/ordered_initializers."
72
+
73
+ f.puts <<~EOF
74
+
75
+ return if Gem::Specification.all_names.any? { |gem_name| gem_name.match(/ordered_initializers/) }
76
+
77
+ abort('`ordered_initializers` gem could not be found.
78
+ You need to either reinstall the gem, or move to this folder all the files from config/ordered_initializer then delete config/initializers.yml as well as this file.')
79
+ EOF
80
+ end
81
+ end
82
+
83
+ move_initializers!
84
+ add_placeholder
85
+ update_initializers_yml!
86
+ end
87
+ end
88
+
89
+ task environment: %w[ordered_initializers:install]
@@ -0,0 +1,3 @@
1
+ module OrderedInitializers
2
+ VERSION = "0.3.2".freeze
3
+ end
@@ -0,0 +1,19 @@
1
+ require "ordered_initializers/rails"
2
+
3
+ module OrderedInitializers
4
+ class << self
5
+ attr_accessor :initializer_files
6
+ end
7
+
8
+ module_function
9
+
10
+ def go
11
+ initializer_files.each do |file|
12
+ load_config_initializer(file)
13
+ end
14
+ end
15
+
16
+ def load_config_initializer(initializer)
17
+ load initializer
18
+ end
19
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,3 @@
1
+ - file1.rb
2
+ - file2.rb
3
+ - file3.rb
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ describe OrderedInitializers::Parser do
4
+ let(:parser) { described_class.new }
5
+
6
+ before do
7
+ Dir.chdir(File.expand_path("../../fixtures", __FILE__))
8
+ allow(Rails).to receive(:root).and_return Pathname.new(File.expand_path("../../fixtures", __FILE__))
9
+ end
10
+
11
+ describe ".initializer_files" do
12
+ before do
13
+ allow(described_class).to receive(:path).and_return("initializers.yml")
14
+ end
15
+
16
+ subject(:initializer_files) { parser.initializer_files }
17
+
18
+ it "returns parsed content as an Array" do
19
+ expect(initializer_files).to eq(["file1.rb", "file2.rb", "file3.rb"])
20
+ end
21
+ end
22
+
23
+ describe ".yml_file" do
24
+ before do
25
+ allow(described_class).to receive(:path).and_return("initializers.yml")
26
+ end
27
+
28
+ subject(:yml_file) { parser.send(:yml_file) }
29
+
30
+ it "returns content of yaml file" do
31
+ expect(yml_file).to eq("- file1.rb\n- file2.rb\n- file3.rb\n")
32
+ end
33
+ end
34
+
35
+ describe "#path" do
36
+ subject(:path) { described_class.path }
37
+
38
+ it "returns the default path" do
39
+ expect(path).to eq("config/initializers.yml")
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,71 @@
1
+ require "spec_helper"
2
+ require "rails"
3
+ require "ordered_initializers/rails"
4
+
5
+ # Fake watcher for Spring
6
+ class SpecWatcher
7
+ attr_reader :items
8
+
9
+ def initialize
10
+ @items = []
11
+ end
12
+
13
+ def add(*items)
14
+ @items |= items
15
+ end
16
+ end
17
+
18
+ describe OrderedInitializers::Railtie do
19
+ before do
20
+ Rails.env = "test"
21
+ allow(Rails).to receive(:root).and_return Pathname.new(File.expand_path("../../fixtures", __FILE__))
22
+
23
+ Rails.application = double(:application)
24
+ end
25
+
26
+ after do
27
+ Rails.application = nil
28
+ end
29
+
30
+ describe "before_initialize" do
31
+ before do
32
+ allow(OrderedInitializers::Parser).to receive(:path).and_return(path)
33
+ end
34
+
35
+ context "when the initializers.yml file is present" do
36
+ let(:path) { "initializers.yml" }
37
+
38
+ it "calls OrderedInitializers.go" do
39
+ expect(described_class.instance).to receive(:load_initializer_file).and_call_original
40
+ expect(OrderedInitializers).to receive(:go)
41
+
42
+ ActiveSupport.run_load_hooks(:before_initialize)
43
+ end
44
+ end
45
+
46
+ fcontext "when the initializers.yml file is missing" do
47
+ let(:path) { "foobar.yml" }
48
+
49
+ it "does not call OrderedInitializers.go" do
50
+ expect(described_class.instance).to receive(:load_initializer_file).and_call_original
51
+ expect(OrderedInitializers).not_to receive(:go)
52
+
53
+ expect(::Kernel).to receive(:warn).with(/Skipping ordered_initializers/)
54
+
55
+ ActiveSupport.run_load_hooks(:before_initialize)
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "#load_initializer_file" do
61
+ subject(:load_initializer_file) { described_class.instance.load_initializer_file }
62
+
63
+ it "sets OrderedInitializers.initializer_files" do
64
+ allow(described_class.instance).to receive(:parsed).and_return(["fileA.rb", "fileB.rb", "fileC.rb"])
65
+
66
+ load_initializer_file
67
+
68
+ expect(OrderedInitializers.initializer_files).to eq(["fileA.rb", "fileB.rb", "fileC.rb"])
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,20 @@
1
+ require "spec_helper"
2
+
3
+ describe OrderedInitializers do
4
+ describe ".go" do
5
+ subject(:go) { OrderedInitializers.go }
6
+
7
+ it "calls load_config_initializer as many times as there are entry in initializer_files" do
8
+ OrderedInitializers.initializer_files = Array.new(5)
9
+ expect(OrderedInitializers).to receive(:load_config_initializer).exactly(5).times
10
+
11
+ go
12
+ end
13
+
14
+ it "raises an error if one initializer file is missing" do
15
+ OrderedInitializers.initializer_files = ["foo.rb"]
16
+
17
+ expect { go }.to raise_error(LoadError, /cannot load such file -- foo.rb/)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ require "ordered_initializers"
2
+
3
+ RSpec::Mocks.configuration.allow_message_expectations_on_nil = true
4
+
5
+ RSpec.configure do |config|
6
+ # Reinit OrderedInitializers.initializer_files value
7
+ config.before { OrderedInitializers.initializer_files = nil }
8
+ config.after { OrderedInitializers.initializer_files = nil }
9
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ordered_initializers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin BOCCARA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-10-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '5.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: appraisal
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: standard
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: Run your Rails application initializers file in the order you want.
90
+ email: boccarab@gmail.com
91
+ executables: []
92
+ extensions: []
93
+ extra_rdoc_files:
94
+ - README.md
95
+ files:
96
+ - LICENSE
97
+ - README.md
98
+ - lib/ordered_initializers.rb
99
+ - lib/ordered_initializers/parser.rb
100
+ - lib/ordered_initializers/rails.rb
101
+ - lib/ordered_initializers/tasks/install.rake
102
+ - lib/ordered_initializers/version.rb
103
+ - spec/fixtures/initializers.yml
104
+ - spec/fixtures/initializers/file1.rb
105
+ - spec/fixtures/initializers/file2.rb
106
+ - spec/fixtures/initializers/file3.rb
107
+ - spec/ordered_initializers/parser_spec.rb
108
+ - spec/ordered_initializers/rails_spec.rb
109
+ - spec/ordered_initializers_spec.rb
110
+ - spec/spec_helper.rb
111
+ homepage: https://rubygems.org/gems/ordered_initializers
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options:
117
+ - "--charset=UTF-8"
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubygems_version: 3.3.26
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Run your Rails application initializers file in the order you want.
135
+ test_files:
136
+ - spec/fixtures/initializers.yml
137
+ - spec/fixtures/initializers/file1.rb
138
+ - spec/fixtures/initializers/file2.rb
139
+ - spec/fixtures/initializers/file3.rb
140
+ - spec/ordered_initializers/parser_spec.rb
141
+ - spec/ordered_initializers/rails_spec.rb
142
+ - spec/ordered_initializers_spec.rb
143
+ - spec/spec_helper.rb