bootinq 1.0.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 729bf1c9b83d3e11258f451f220542d46836ffc9
4
- data.tar.gz: fc860ed30c15acc2046e2e33074d4097004aa4fc
2
+ SHA256:
3
+ metadata.gz: 2132126092e2938ead320330a8866a61e496e47683b1a88c7ec3f8b15d3f1f2b
4
+ data.tar.gz: 3eee6dabbc507a0d8234d2edc64461eddca66259b3df04c76799cbfffbbb94f8
5
5
  SHA512:
6
- metadata.gz: f2aa46cef37e5b173a9efb010bbc3fcc616f324ddb8ed52938e8303ab400ff707baf6bddfa3135b918c7adcf791463c59021a257efa0589b8888435877d0c2ae
7
- data.tar.gz: b1f55595d4f7611014c547831d1b34b5dae00ba479802099de799f732b2665afefb94ecb9ae7377b6b355a427c568957eeffc9783e4cf26dca0dcb8d6747b3c3
6
+ metadata.gz: e2b45226f655ee94a841ec79f70934af6948427ba97e4a1269a4371693b9c3d50bbd9857db2b2b7a914532cc04a571176c41dbeb7f44bbd5979b4464562e5dbe
7
+ data.tar.gz: cd87ee76d07175f774202a45c20875d5e1c9a18ab2e0f469e75b2d4a9039520fda537ba29d12f05f67e5e1704b8797a4296ba5c277e56da14a1982731ff7678f
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,17 +1,39 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "yaml"
2
4
  require "singleton"
3
5
  require "forwardable"
4
6
  require "bootinq/component"
7
+ require "bootinq/switch"
5
8
 
6
9
  # = Bootinq
7
10
  #
8
11
  # == Installation
9
12
  #
13
+ # === Ruby on Rails
14
+ #
10
15
  # 1. Insert <tt>require "bootinq"</tt> in the top of <tt>config/application.rb</tt>
11
16
  #
12
17
  # 2. Find <tt>Bundler.require(*Rails.groups)</tt> line below and replace it
13
18
  # with the <tt>Bootinq.require</tt>.
14
19
  #
20
+ # === Other
21
+ #
22
+ # 1. Locate <tt>Bundler.require(...)</tt> in your app and insert <tt>require "bootinq"</tt> above.
23
+ #
24
+ # 2. Replace located <tt>Bundler.require(...)</tt> line with the <tt>Bootinq.require(...)</tt>.
25
+ #
26
+ # For example, if you are using Grape:
27
+ #
28
+ # # config/application.rb
29
+ #
30
+ # require 'boot'
31
+ # require 'bootinq'
32
+ #
33
+ # # Bundler.require :default, ENV['RACK_ENV']
34
+ # Bootinq.require :default, ENV['RACK_ENV'], verbose: true
35
+ # ...
36
+ #
15
37
  # == Example <tt>config/bootinq.yml</tt>:
16
38
  #
17
39
  # env_key: BOOTINQ
@@ -24,7 +46,6 @@ require "bootinq/component"
24
46
  # a: :api
25
47
  # f: :engine
26
48
  class Bootinq
27
- extend SingleForwardable
28
49
  include Singleton
29
50
 
30
51
  DEFAULT = {
@@ -34,62 +55,125 @@ class Bootinq
34
55
  "mount" => {}
35
56
  }.freeze
36
57
 
58
+ class << self
59
+ protected def delegated(sym) # :no-doc:
60
+ location = caller_locations(1, 1).first
61
+ file, line = location.path, location.lineno
62
+ definiton = %(def self.#{sym}(*args, &block); instance.#{sym}(*args, &block); end)
63
+ class_eval definiton, file, line
64
+ end
65
+ end
66
+
67
+ # :call-seq:
68
+ # Bootinq.require(*groups, verbose: false, &block)
69
+ #
37
70
  # The helper method to bootstrap the Bootinq.
38
- # Sets the BOOTINQ_PATH enviroment variable, yields optional block in
39
- # the own instance's binding and, finally, requires selected bundler groups.
40
- def self.require(*groups, verbose: false, &block) # :yields:
71
+ # Sets the BOOTINQ_PATH enviroment variable if it is missing,
72
+ # invokes the <tt>Bootinq.setup</tt> method with the given verbose key argument & block,
73
+ # and, finally, gets Bundler to require the given groups.
74
+ def self.require(*groups, verbose: false, &block) # :yields: Bootinq.instance
41
75
  ENV['BOOTINQ_PATH'] ||= File.expand_path('../bootinq.yml', caller_locations(1..1)[0].path)
42
76
 
43
- puts "Bootinq: loading components #{instance.components.join(', ')}" if verbose
44
-
45
- instance.instance_exec(&block) if block_given?
77
+ setup(verbose: verbose, &block)
46
78
 
47
79
  Bundler.require(*instance.groups(*groups))
48
80
  end
49
81
 
50
- private_class_method def self.new # :nodoc:
51
- super.freeze
82
+ # :call-seq:
83
+ # Bootinq.setup(verbose: false, &block) -> true or false
84
+ #
85
+ # Initializes itself. To track inquired components use <tt>verbose: true</tt> key argument.
86
+ # Optionally yields block within the own instance's binding.
87
+ def self.setup(verbose: false, &block) # :yields: Bootinq.instance
88
+ instance
89
+ puts "Bootinq: loading components #{instance.components.join(', ')}" if verbose
90
+ instance.instance_exec(&block) if block_given?
91
+ instance
52
92
  end
53
93
 
94
+ attr_reader :flags
95
+ attr_reader :components
54
96
 
55
- attr_reader :flags, :components
97
+ delegated :flags
98
+ delegated :components
56
99
 
57
- def initialize
58
- config = YAML.safe_load(File.read(ENV.fetch('BOOTINQ_PATH')), [Symbol]).
59
- merge!(DEFAULT) { |_,l,r| l.nil? ? r : l }
100
+ def initialize # :no-doc:
101
+ config_path = ENV.fetch('BOOTINQ_PATH')
102
+ config = YAML.safe_load(File.read(config_path), [Symbol])
103
+ config.merge!(DEFAULT) { |_, l, r| l.nil? ? r : l }
60
104
 
61
105
  @_value = ENV.fetch(config['env_key']) { config['default'] }
62
- @_neg = @_value.start_with?("-", "^")
106
+ @_neg = @_value.start_with?(?-, ?^)
63
107
  @flags = []
64
108
  @components = []
65
109
 
66
- config['parts'].each { |flag, name| enable_component(flag) { Component.new(name) } }
67
- config['mount'].each { |flag, name| enable_component(flag) { Mountable.new(name) } }
110
+ config['parts'].each { |flag, name| enable_component(name, flag: flag) }
111
+ config['mount'].each { |flag, name| enable_component(name, flag: flag, as: Mountable) }
112
+ end
113
+
114
+ # :call-seq:
115
+ # Bootinq.enable_component(name, flag: [, as: Component])
116
+ #
117
+ delegated def enable_component(name, flag:, as: Component)
118
+ if @_neg ^ @_value.include?(flag)
119
+ @flags << flag
120
+ @components << as.new(name)
121
+ end
68
122
  end
69
123
 
70
- # Checks if a given gem (i.e. a gem group) is enabled
71
- def enabled?(gem_name)
72
- components.include?(gem_name)
124
+ # :call-seq:
125
+ # Bootinq.enabled?(name) -> true or false
126
+ #
127
+ # Checks if a component with the given name (i.e. the same gem group)
128
+ # is enabled
129
+ delegated def enabled?(name)
130
+ components.include?(name)
73
131
  end
74
132
 
133
+ # :call-seq:
134
+ # Bootinq.component(name) -> Bootinq::Component
135
+ # Bootinq[name] -> Bootinq::Component
136
+ #
75
137
  # Returns a <tt>Bootinq::Component</tt> object by its name
76
- def component(key)
77
- components[components.index(key)]
138
+ delegated def component(name)
139
+ components[components.index(name)]
78
140
  end
79
141
 
80
142
  alias :[] :component
143
+ delegated :[]
81
144
 
82
- # Enums each mountable component
83
- def each_mountable # :yields:
84
- return to_enum(__method__) unless block_given?
85
- components.each { |part| yield(part) if part.mountable? }
145
+ # :call-seq:
146
+ # Bootinq.each_mountable { |part| block } -> Array
147
+ # Bootinq.each_mountable -> Enumerator
148
+ #
149
+ # Calls the given block once for each enabled mountable component
150
+ # passing that part as a parameter. Returns the array of all mountable components.
151
+ #
152
+ # If no block is given, an Enumerator is returned.
153
+ delegated def each_mountable(&block) # :yields: part
154
+ components.select(&:mountable?).each(&block)
86
155
  end
87
156
 
88
- # Invokes <tt>Rails.groups</tt> method within enabled Bootinq's groups
89
- def groups(*list)
90
- Rails.groups(*components.map(&:group), *list)
157
+ # :call-seq:
158
+ # Bootinq.groups(*groups)
159
+ #
160
+ # Merges enabled Bootinq's groups with the given groups and, if loaded with Rails,
161
+ # passes them to <tt>Rails.groups</tt> method, otherwise just returns the merged list
162
+ # to use with <tt>Bundler.require</tt>.
163
+ delegated def groups(*groups)
164
+ groups.unshift(*components.map(&:group))
165
+ if defined?(Rails)
166
+ Rails.groups(*groups)
167
+ else
168
+ groups
169
+ end
91
170
  end
92
171
 
172
+ # :call-seq:
173
+ # Bootinq.on(name) { block } -> true or false
174
+ # Bootinq.on(any: [names]) { block } -> true or false
175
+ # Bootinq.on(all: [names]) { block } -> true or false
176
+ #
93
177
  # Takes a component's name or single-key options hash as an argument and
94
178
  # yields a given block if the target components are enabled.
95
179
  #
@@ -108,21 +192,61 @@ class Bootinq
108
192
  # Bootinq.on all: %i(frontend backend) do
109
193
  # # do something when frontend and backend are enabled
110
194
  # end
111
- def on(name = nil, any: nil, all: nil) # :yields:
112
- if (any && all) || (name && (any || all))
113
- raise ArgumentError, "expected single argument or one of keywords: `all' or `any'"
114
- elsif name
115
- yield if enabled?(name)
116
- elsif any
117
- yield if any.any? { |part| enabled?(part) }
118
- elsif all
119
- yield if all.all? { |part| enabled?(part) }
120
- else
195
+ delegated def on(name = nil, any: nil, all: nil) # :yields:
196
+ if name.nil? && any.nil? && all.nil?
121
197
  raise ArgumentError, "wrong arguments (given 0, expected 1)"
198
+ elsif (any && all) || (name && (any || all))
199
+ raise ArgumentError, "expected single argument or one of keywords: `all' or `any'"
122
200
  end
201
+
202
+ is_matched =
203
+ name ? enabled?(name) :
204
+ any ? on_any(*any) :
205
+ all ? on_all(*all) : false
206
+ yield if is_matched
207
+ is_matched
208
+ end
209
+
210
+ # :call-seq:
211
+ # Bootinq.on_all(*names) { block } -> true or false
212
+ #
213
+ # Takes a list of component names and yields a given block (optionally)
214
+ # if all of them are enabled. Returns boolean matching status.
215
+ delegated def on_all(*parts) # :yields:
216
+ is_matched = parts.all? { |p| enabled?(p) }
217
+ yield if is_matched && block_given?
218
+ is_matched
219
+ end
220
+
221
+ # :call-seq:
222
+ # Bootinq.on_all(*names) { block } -> true or false
223
+ #
224
+ # Takes a list of component names and yields a given block (optionally)
225
+ # if any of them are enabled. Returns boolean matching status.
226
+ delegated def on_any(*parts) # :yields:
227
+ is_matched = parts.any? { |p| enabled?(p) }
228
+ yield if is_matched && block_given?
229
+ is_matched
230
+ end
231
+
232
+ # :call-seq:
233
+ # Bottinq.switch(*parts) { block } -> nil
234
+ #
235
+ # Collector method.
236
+ #
237
+ # Example:
238
+ #
239
+ # Bootinq.switch do |part|
240
+ # part.frontend { … }
241
+ # part.backend { … }
242
+ # end
243
+ delegated def switch # :yields: Bootinq::Switch.new
244
+ yield(Switch.new)
245
+ nil
123
246
  end
124
247
 
125
- def freeze # :no-doc:
248
+ # Freezes every instance variables and the instance itself.
249
+ def freeze
126
250
  @_value.freeze
127
251
  @_neg.freeze
128
252
  @flags.freeze
@@ -130,12 +254,9 @@ class Bootinq
130
254
  super
131
255
  end
132
256
 
133
- def_delegators "instance", *instance_methods(false)
134
-
135
- private def enable_component(flag) # :yields:
136
- if @_neg ^ @_value.include?(flag)
137
- @flags << flag
138
- @components << yield
139
- end
257
+ def self.new
258
+ super.freeze
140
259
  end
260
+
261
+ private_class_method :new
141
262
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Bootinq
2
4
  class Component
3
5
  attr_reader :intern, :id2name, :group
@@ -18,6 +20,17 @@ class Bootinq
18
20
  false
19
21
  end
20
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
+
21
34
  def == other
22
35
  case other
23
36
  when String then other == @id2name
@@ -26,26 +39,44 @@ class Bootinq
26
39
  end
27
40
  end
28
41
 
29
- def inspect
30
- @intern.inspect
42
+ def ===(other)
43
+ case other
44
+ when String then other === @id2name
45
+ when Symbol then other === @intern
46
+ else super
47
+ end
31
48
  end
32
49
 
33
- def engine
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
34
56
  end
35
57
 
36
- def module_name
37
- @id2name.camelcase.to_sym
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
38
64
  end
39
65
 
40
- def respond_to_missing?(method_name, include_all=false)
41
- @intern.respond_to?(method_name, include_all)
42
- end
66
+ %i(inspect to_proc __id__ hash).
67
+ each { |sym| class_eval %(def #{sym}; @intern.#{sym}; end), __FILE__, __LINE__ + 1 }
43
68
 
44
- private
69
+ %i(encoding empty? length).
70
+ each { |sym| class_eval %(def #{sym}; @id2name.#{sym}; end), __FILE__, __LINE__ + 1 }
45
71
 
46
- def method_missing(method_name, *args, &blk)
47
- @intern.respond_to?(method_name) ? @intern.public_send(method_name, *args, &blk) : super
48
- end
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
49
80
  end
50
81
 
51
82
  class Mountable < Component
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Bootinq
4
+ class Switch < ::BasicObject # :no-doc:
5
+ undef_method :==
6
+ undef_method :equal?
7
+
8
+ def raise(*args) # :no-doc:
9
+ ::Object.send(:raise, *args)
10
+ end
11
+
12
+ def method_missing(name, *)
13
+ if ::Bootinq.enabled?(name)
14
+ yield()
15
+ else
16
+ nil
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Bootinq
2
- VERSION = "1.0.0"
4
+ VERSION = "1.3.1"
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: 1.0.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-14 00:00:00.000000000 Z
11
+ date: 2020-06-27 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
@@ -57,8 +58,8 @@ files:
57
58
  - lib/bootinq.rb
58
59
  - lib/bootinq.yml
59
60
  - lib/bootinq/component.rb
61
+ - lib/bootinq/switch.rb
60
62
  - lib/bootinq/version.rb
61
- - lib/tasks/bootinq_tasks.rake
62
63
  homepage: https://github.com/estum/bootinq
63
64
  licenses:
64
65
  - MIT
@@ -78,10 +79,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
79
  - !ruby/object:Gem::Version
79
80
  version: '0'
80
81
  requirements: []
81
- rubyforge_project:
82
- rubygems_version: 2.5.1
82
+ rubygems_version: 3.0.3
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