bootinq 0.3.0 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: a8647f4b798663b8511a3d2c7a9527b93756b9ca
4
- data.tar.gz: 35bd6157a95de1bd4df9648896d08939401281cb
2
+ SHA256:
3
+ metadata.gz: 266e204b1e16ba81aaaf17e2ab6b638f2c33b1717a9c728e65332b6b2274e98d
4
+ data.tar.gz: 0f8210ddf0aae40d21ccc68b3964df30b7102ea35cb76c04779e3a2f48d0f9dd
5
5
  SHA512:
6
- metadata.gz: 8cf0fc21e41449527782916a81a3dfb2749f6e9f91dda590d381a5b5dd15bc31c5d15b6216fdf1cb717424cd83a069b09a3fab213a657a041700ae313fe9a8df
7
- data.tar.gz: add219d5df6e7a54aae76552de7be7f5509d79fdb51541399f3705f3c0f025de28b19b396374b842e92fd6ae96591bb2284b66acb75ceff8a3b500477d83f748
6
+ metadata.gz: d8d3172c9a7a38e335585b0b1a35dc7bdaccbb993076095cd414a317f891d0506e6b0995b5e7eb4cdea9f885553658cd175219273a788db797994d63eca59a44
7
+ data.tar.gz: 182655c54c91a0a4cac2d7c17e5750e59e8941309a9ce492faddc1204f30e72a90c847f5e48e23ffb549cc9dc60ad4ed0c0b064eb92348960a6b85286380321b
data/.gitignore CHANGED
@@ -13,4 +13,5 @@
13
13
  /spec/dummy/log/*.log
14
14
  /spec/dummy/tmp/
15
15
  /spec/dummy/.sass-cache
16
- .rake_t_cache
16
+ .rake_t_cache
17
+ .ruby-version
data/Gemfile CHANGED
@@ -10,7 +10,7 @@ gemspec
10
10
  # Git. Remember to move these dependencies to your gemspec before releasing
11
11
  # your gem to rubygems.org.
12
12
 
13
- gem 'rails', '~> 4.2'
13
+ gem 'rails', '>= 5.0'
14
14
  gem 'sqlite3'
15
15
 
16
16
  # To use a debugger
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Anton
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -15,9 +15,9 @@ And then execute:
15
15
  $ bundle
16
16
 
17
17
 
18
- ## Usage
18
+ ## Get started
19
19
 
20
- There are few steps to setup partial gem booting in a Rails application using Bootinq:
20
+ There are few steps to setup partial gem booting using Bootinq in any ruby application:
21
21
 
22
22
  ### 1. Declare loadable parts
23
23
 
@@ -60,7 +60,9 @@ group :console_boot do
60
60
  end
61
61
  ```
62
62
 
63
- ### 3. Change config/application.rb
63
+ ## Ruby on Rails
64
+
65
+ ### 3. Swap Bundle.require to Bootinq.require
64
66
 
65
67
  Insert `require "bootinq"` to the top of `config/application.rb` file and replace `Bundler.require(*Rails.groups)` with the `Bootinq.require`:
66
68
 
@@ -74,11 +76,10 @@ require 'rails/all'
74
76
  require 'bootinq'
75
77
 
76
78
  # With no additional gem groups:
77
- Bootinq.require
79
+ Bootinq.require(verbose: true)
78
80
  # otherwise, set them like in <tt>Bundle.require(*Rails.groups(*groups))</tt>:
79
81
  # Bootinq.require(:assets => %w(development test))
80
82
 
81
- puts "* Bootinq: loading components #{Bootinq.components * ', '}"
82
83
  ```
83
84
 
84
85
  #### Separate load rails components with Bootinq
@@ -131,6 +132,24 @@ api: env BOOTINQ=a MAX_THREADS=128 bundle exec puma -w 4
131
132
  admin: env BOOTINQ=z bundle exec puma
132
133
  ```
133
134
 
135
+ ## Usage with other frameworks
136
+
137
+ 3. Locate `Bundler.require(...)` in your app and insert `require "bootinq"` above it.
138
+
139
+ 4. Replace located `Bundler.require(...)` line with the `Bootinq.require(...)`.
140
+
141
+ For example, if you are using Grape:
142
+
143
+ ```ruby
144
+ # config/application.rb
145
+
146
+ require 'boot'
147
+ require 'bootinq'
148
+
149
+ # Bundler.require :default, ENV['RACK_ENV']
150
+ Bootinq.require :default, ENV['RACK_ENV'], verbose: true
151
+
152
+
134
153
  ## Development
135
154
 
136
155
  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.
@@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_development_dependency "bundler", "~> 1.10"
23
- spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "rake"
24
24
  end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "yaml"
2
- require "erb"
3
4
  require "singleton"
4
5
  require "forwardable"
5
6
  require "bootinq/component"
@@ -8,11 +9,30 @@ require "bootinq/component"
8
9
  #
9
10
  # == Installation
10
11
  #
12
+ # === Ruby on Rails
13
+ #
11
14
  # 1. Insert <tt>require "bootinq"</tt> in the top of <tt>config/application.rb</tt>
12
15
  #
13
16
  # 2. Find <tt>Bundler.require(*Rails.groups)</tt> line below and replace it
14
17
  # with the <tt>Bootinq.require</tt>.
15
18
  #
19
+ # === Other
20
+ #
21
+ # 1. Locate <tt>Bundler.require(...)</tt> in your app and insert <tt>require "bootinq"</tt> above.
22
+ #
23
+ # 2. Replace located <tt>Bundler.require(...)</tt> line with the <tt>Bootinq.require(...)</tt>.
24
+ #
25
+ # For example, if you are using Grape:
26
+ #
27
+ # # config/application.rb
28
+ #
29
+ # require 'boot'
30
+ # require 'bootinq'
31
+ #
32
+ # # Bundler.require :default, ENV['RACK_ENV']
33
+ # Bootinq.require :default, ENV['RACK_ENV'], verbose: true
34
+ # ...
35
+ #
16
36
  # == Example <tt>config/bootinq.yml</tt>:
17
37
  #
18
38
  # env_key: BOOTINQ
@@ -25,93 +45,201 @@ require "bootinq/component"
25
45
  # a: :api
26
46
  # f: :engine
27
47
  class Bootinq
28
- NEG = '-'.freeze
48
+ include Singleton
49
+
29
50
  DEFAULT = {
30
51
  "env_key" => 'BOOTINQ',
31
52
  "default" => '',
32
53
  "parts" => {},
33
54
  "mount" => {}
34
- }
55
+ }.freeze
35
56
 
36
- attr_reader :flags, :components
57
+ class << self
58
+ protected def delegated(sym) # :no-doc:
59
+ location = caller_locations(1, 1).first
60
+ file, line = location.path, location.lineno
61
+ definiton = %(def self.#{sym}(*args, &block); instance.#{sym}(*args, &block); end)
62
+ class_eval definiton, file, line
63
+ end
64
+ end
37
65
 
38
- include Singleton
39
- extend SingleForwardable
66
+ # :call-seq:
67
+ # Bootinq.require(*groups, verbose: false, &block)
68
+ #
69
+ # The helper method to bootstrap the Bootinq.
70
+ # Sets the BOOTINQ_PATH enviroment variable if it is missing,
71
+ # invokes the <tt>Bootinq.setup</tt> method with the given verbose key argument & block,
72
+ # and, finally, gets Bundler to require the given groups.
73
+ def self.require(*groups, verbose: false, &block) # :yields: Bootinq.instance
74
+ ENV['BOOTINQ_PATH'] ||= File.expand_path('../bootinq.yml', caller_locations(1..1)[0].path)
75
+
76
+ setup(verbose: verbose, &block)
77
+
78
+ Bundler.require(*instance.groups(*groups))
79
+ end
40
80
 
41
- def initialize
42
- config = YAML.load(File.read(ENV['BOOTINQ_PATH']))
43
- config.reject! { |_, v| v.nil? }
44
- config.reverse_merge!(DEFAULT)
81
+ # :call-seq:
82
+ # Bootinq.setup(verbose: false, &block) -> true or false
83
+ #
84
+ # Initializes itself. To track inquired components use <tt>verbose: true</tt> key argument.
85
+ # Optionally yields block within the own instance's binding.
86
+ def self.setup(verbose: false, &block) # :yields: Bootinq.instance
87
+ instance
88
+ puts "Bootinq: loading components #{instance.components.join(', ')}" if verbose
89
+ instance.instance_exec(&block) if block_given?
90
+ instance
91
+ end
45
92
 
46
- config['parts'].merge!(config['mount'])
93
+ attr_reader :flags
94
+ attr_reader :components
47
95
 
48
- value = ENV[config['env_key']] || config['default'].to_s
49
- neg = value.start_with?(NEG)
96
+ delegated :flags
97
+ delegated :components
50
98
 
51
- flags = []
52
- parts = []
99
+ def initialize # :no-doc:
100
+ config_path = ENV.fetch('BOOTINQ_PATH')
101
+ config = YAML.safe_load(File.read(config_path), [Symbol])
102
+ config.merge!(DEFAULT) { |_, l, r| l.nil? ? r : l }
53
103
 
54
- config['parts'].each do |flag, name|
55
- if neg ^ value[flag]
56
- flags << flag
57
- parts << Component.new(name, mountable: config['mount'].key?(flag))
58
- end
59
- end
104
+ @_value = ENV.fetch(config['env_key']) { config['default'] }
105
+ @_neg = @_value.start_with?(?-, ?^)
106
+ @flags = []
107
+ @components = []
60
108
 
61
- @_value = value.freeze
62
- @_neg = neg.freeze
63
- @flags = flags.freeze
64
- @components = parts.freeze
109
+ config['parts'].each { |flag, name| enable_component(name, flag: flag) }
110
+ config['mount'].each { |flag, name| enable_component(name, flag: flag, as: Mountable) }
111
+ end
112
+
113
+ # :call-seq:
114
+ # Bootinq.enable_component(name, flag: [, as: Component])
115
+ #
116
+ delegated def enable_component(name, flag:, as: Component)
117
+ if @_neg ^ @_value.include?(flag)
118
+ @flags << flag
119
+ @components << as.new(name)
120
+ end
65
121
  end
66
122
 
67
- # Checks if a given gem (i.e. a gem group) is enabled
68
- def enabled?(gem_name)
69
- components.include?(gem_name)
123
+ # :call-seq:
124
+ # Bootinq.enabled?(name) -> true or false
125
+ #
126
+ # Checks if a component with the given name (i.e. the same gem group)
127
+ # is enabled
128
+ delegated def enabled?(name)
129
+ components.include?(name)
70
130
  end
71
131
 
132
+ # :call-seq:
133
+ # Bootinq.component(name) -> Bootinq::Component
134
+ # Bootinq[name] -> Bootinq::Component
135
+ #
72
136
  # Returns a <tt>Bootinq::Component</tt> object by its name
73
- def component(key)
74
- components[components.index(key)]
137
+ delegated def component(name)
138
+ components[components.index(name)]
75
139
  end
76
140
 
77
- # Enums each mountable component
78
- def each_mountable
79
- return to_enum(__method__) unless block_given?
80
- components.each { |c| yield(c) if c.mountable? }
141
+ alias :[] :component
142
+ delegated :[]
143
+
144
+ # :call-seq:
145
+ # Bootinq.each_mountable { |part| block } -> Array
146
+ # Bootinq.each_mountable -> Enumerator
147
+ #
148
+ # Calls the given block once for each enabled mountable component
149
+ # passing that part as a parameter. Returns the array of all mountable components.
150
+ #
151
+ # If no block is given, an Enumerator is returned.
152
+ delegated def each_mountable(&block) # :yields: part
153
+ components.select(&:mountable?).each(&block)
81
154
  end
82
155
 
83
- # Invokes <tt>Rails.groups</tt> method within enabled Bootinq's groups
84
- def groups(*list)
85
- Rails.groups(*components.map(&:group), *list)
156
+ # :call-seq:
157
+ # Bootinq.groups(*groups)
158
+ #
159
+ # Merges enabled Bootinq's groups with the given groups and, if loaded with Rails,
160
+ # passes them to <tt>Rails.groups</tt> method, otherwise just returns the merged list
161
+ # to use with <tt>Bundler.require</tt>.
162
+ delegated def groups(*groups)
163
+ groups.unshift(*components.map(&:group))
164
+ if defined?(Rails)
165
+ Rails.groups(*groups)
166
+ else
167
+ groups
168
+ end
86
169
  end
87
170
 
88
- # Yields the given block if any of given components is enabled.
171
+ # :call-seq:
172
+ # Bootinq.on(name) { block } -> true or false
173
+ # Bootinq.on(any: [names]) { block } -> true or false
174
+ # Bootinq.on(all: [names]) { block } -> true or false
175
+ #
176
+ # Takes a component's name or single-key options hash as an argument and
177
+ # yields a given block if the target components are enabled.
178
+ #
179
+ # See examples for a usage.
89
180
  #
90
181
  # ==== Example:
91
182
  #
92
183
  # Bootinq.on :frontend do
93
184
  # # make frontend thing...
94
185
  # end
95
- def on(*names) # :yields:
96
- if names.any? { |name| enabled?(name) }
97
- yield
186
+ #
187
+ # Bootinq.on any: %i(frontend backend) do
188
+ # # do something when frontend or backend is enabled
189
+ # end
190
+ #
191
+ # Bootinq.on all: %i(frontend backend) do
192
+ # # do something when frontend and backend are enabled
193
+ # end
194
+ delegated def on(name = nil, any: nil, all: nil) # :yields:
195
+ if name.nil? && any.nil? && all.nil?
196
+ raise ArgumentError, "wrong arguments (given 0, expected 1)"
197
+ elsif (any && all) || (name && (any || all))
198
+ raise ArgumentError, "expected single argument or one of keywords: `all' or `any'"
98
199
  end
200
+
201
+ is_matched =
202
+ name ? enabled?(name) :
203
+ any ? on_any(*any) :
204
+ all ? on_all(*all) : false
205
+ yield if is_matched
206
+ is_matched
99
207
  end
100
208
 
101
- def_delegators "instance", *instance_methods(false)
209
+ # :call-seq:
210
+ # Bootinq.on_all(*names) { block } -> true or false
211
+ #
212
+ # Takes a list of component names and yields a given block (optionally)
213
+ # if all of them are enabled. Returns boolean matching status.
214
+ delegated def on_all(*parts) # :yields:
215
+ is_matched = parts.all? { |p| enabled?(p) }
216
+ yield if is_matched && block_given?
217
+ is_matched
218
+ end
102
219
 
103
- class << self
104
- # The helper method to bootstrap the Bootinq.
105
- # Sets the BOOTINQ_PATH enviroment variable, yields optional block in
106
- # the own instance's binding and, finally, requires selected bundler groups.
107
- def require(*groups, &block) # :yields:
108
- ENV['BOOTINQ_PATH'] ||= File.expand_path('../bootinq.yml', caller_locations(1..1)[0].path)
109
- instance.instance_exec(&block) if block_given?
110
- Bundler.require(*instance.groups(*groups))
111
- end
220
+ # :call-seq:
221
+ # Bootinq.on_all(*names) { block } -> true or false
222
+ #
223
+ # Takes a list of component names and yields a given block (optionally)
224
+ # if any of them are enabled. Returns boolean matching status.
225
+ delegated def on_any(*parts) # :yields:
226
+ is_matched = parts.any? { |p| enabled?(p) }
227
+ yield if is_matched && block_given?
228
+ is_matched
229
+ end
112
230
 
113
- private def new
114
- super.freeze
115
- end
231
+ # Freezes every instance variables and the instance itself.
232
+ def freeze
233
+ @_value.freeze
234
+ @_neg.freeze
235
+ @flags.freeze
236
+ @components.freeze
237
+ super
116
238
  end
239
+
240
+ def self.new
241
+ super.freeze
242
+ end
243
+
244
+ private_class_method :new
117
245
  end
@@ -1,20 +1,91 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Bootinq
2
- class Component < DelegateClass(Symbol)
3
- attr_reader :gem_name, :mountable, :namespace, :group
4
- alias :to_s :gem_name
5
- alias :mountable? :mountable
6
-
7
- def initialize(name, mountable: false)
8
- super(name)
9
- @gem_name = name.to_s.freeze
10
- @mountable = !!mountable
11
- @group = :"#{gem_name}_boot"
12
- @namespace = :"#{gem_name.camelcase}" if mountable?
4
+ class Component
5
+ attr_reader :intern, :id2name, :group
6
+
7
+ alias :to_sym :intern
8
+ alias :to_s :id2name
9
+ alias :gem_name :id2name
10
+ alias :name :id2name
11
+
12
+ def initialize(intern)
13
+ @intern = intern.to_sym
14
+ @id2name = intern.to_s.freeze
15
+ @group = :"#@id2name\_boot"
13
16
  freeze
14
17
  end
15
18
 
19
+ def mountable?
20
+ false
21
+ end
22
+
23
+ def module_name
24
+ @id2name.camelcase.to_sym
25
+ end
26
+
27
+ def engine
28
+ end
29
+
30
+ def kind_of?(klass)
31
+ super || @intern.kind_of?(klass)
32
+ end
33
+
34
+ def == other
35
+ case other
36
+ when String then other == @id2name
37
+ when Symbol then other == @intern
38
+ else super
39
+ end
40
+ end
41
+
42
+ def ===(other)
43
+ case other
44
+ when String then other === @id2name
45
+ when Symbol then other === @intern
46
+ else super
47
+ end
48
+ end
49
+
50
+ def casecmp(other)
51
+ case other
52
+ when String then @id2name.casecmp(other)
53
+ when Symbol then @intern.casecmp(other)
54
+ when self.class then casecmp(other.to_s)
55
+ end
56
+ end
57
+
58
+ def casecmp?(other)
59
+ case other
60
+ when String then @id2name.casecmp?(other)
61
+ when Symbol then @intern.casecmp?(other)
62
+ when self.class then casecmp?(other.to_s)
63
+ end
64
+ end
65
+
66
+ %i(inspect to_proc __id__ hash).
67
+ each { |sym| class_eval %(def #{sym}; @intern.#{sym}; end), __FILE__, __LINE__ + 1 }
68
+
69
+ %i(encoding empty? length).
70
+ each { |sym| class_eval %(def #{sym}; @id2name.#{sym}; end), __FILE__, __LINE__ + 1 }
71
+
72
+ %i(match match? =~ []).
73
+ each { |sym| class_eval %(def #{sym}(*args); @id2name.#{sym}(*args); end), __FILE__, __LINE__ + 1 }
74
+
75
+ %i(upcase downcase capitalize swapcase succ next).
76
+ each { |sym| class_eval %(def #{sym}; self.class.new(@intern.#{sym}); end), __FILE__, __LINE__ + 1 }
77
+
78
+ alias :slice :[]
79
+ alias :size :length
80
+ end
81
+
82
+ class Mountable < Component
83
+ def mountable?
84
+ true
85
+ end
86
+
16
87
  def engine
17
- Object.const_get(@namespace)::Engine
88
+ Object.const_get(module_name)::Engine
18
89
  end
19
90
  end
20
91
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Bootinq
2
- VERSION = "0.3.0"
4
+ VERSION = "1.2.0"
3
5
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bootinq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-26 00:00:00.000000000 Z
11
+ date: 2020-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.10'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0'
41
41
  description: Allows to select which bundle groups to boot in the current rails process
42
42
  email:
43
43
  - anton.estum@gmail.com
@@ -49,6 +49,7 @@ files:
49
49
  - ".rspec"
50
50
  - ".travis.yml"
51
51
  - Gemfile
52
+ - LICENSE.txt
52
53
  - README.md
53
54
  - Rakefile
54
55
  - bin/console
@@ -58,7 +59,6 @@ files:
58
59
  - lib/bootinq.yml
59
60
  - lib/bootinq/component.rb
60
61
  - lib/bootinq/version.rb
61
- - lib/tasks/bootinq_tasks.rake
62
62
  homepage: https://github.com/estum/bootinq
63
63
  licenses:
64
64
  - MIT
@@ -79,9 +79,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
79
  version: '0'
80
80
  requirements: []
81
81
  rubyforge_project:
82
- rubygems_version: 2.4.8
82
+ rubygems_version: 2.7.6
83
83
  signing_key:
84
84
  specification_version: 4
85
85
  summary: Rails Boot Inquirer
86
86
  test_files: []
87
- has_rdoc:
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :bootinq do
3
- # # Task goes here
4
- # end