Class: Bootinq
- Inherits:
- 
      Object
      
        - Object
- Bootinq
 
- Extended by:
- SingleForwardable
- Includes:
- Mixins, Singleton
- Defined in:
- lib/bootinq.rb,
 lib/bootinq/mixins.rb,
 lib/bootinq/switch.rb,
 lib/bootinq/railtie.rb,
 lib/bootinq/version.rb,
 lib/bootinq/component.rb
Overview
Bootinq
Installation
Ruby on Rails
- insert require "bootinq"on top ofconfig/application.rb;
- find and replace Bundler.require(*Rails.groups)withBootinq.require
Other frameworks
- locate Bundler.require(…)in your app and insertrequire "bootinq"above it;
- replace previosly located Bundler.require(…)line with theBootinq.require(…).
Defined Under Namespace
Modules: Mixins Classes: Component, Mountable, Railtie, Switch
Constant Summary collapse
- DEFAULT =
- { "env_key" => 'BOOTINQ', "default" => '', "parts" => {}, "mount" => {}, "deps" => {} }.freeze 
- ALL =
- %i[* all].freeze 
- VERSION =
- "2.0.1"
Instance Attribute Summary collapse
- 
  
    
      #components  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute components. 
- 
  
    
      #flags  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute flags. 
Class Method Summary collapse
- 
  
    
      .deserialized_config(path: nil)  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    Reads config. 
- 
  
    
      .init(verbose: false, on_ready: nil, &block)  ⇒ instance 
    
    
  
  
  
  
  
  
  
  
  
    Sets BOOTINQ_PATHenviroment variable if it is missing & initializes itself.
- 
  
    
      .require(*groups, **options, &on_ready)  
    
    
  
  
  
  
  
  
  
  
  
    Invokes the Bootinq.init method with the given options and block, then calls Bundler.require with the enabled groups. 
- 
  
    
      .setup(*groups, **options) {|instance| ... } 
    
    
  
  
  
  
  
  
  
  
  
    Invokes the Bootinq.init method with the given options and block, then calls Bundler.require with the enabled groups. 
Instance Method Summary collapse
- #component(name) ⇒ Bootinq::Component (also: #[])
- 
  
    
      #disabled?(name)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Checks if a component with the given name (i.e. the same gem group) is disabled. 
- 
  
    
      #each_mountable  ⇒ Enumerator 
    
    
  
  
  
  
  
  
  
  
  
    Enumerates enabled mountable components. 
- 
  
    
      #enable_component(name, flag:, as: Component) {|name, is_enabled| ... } 
    
    
  
  
  
  
  
  
  
  
  
    Enables the given component if it is required by flag or when another enabled component depends it. 
- 
  
    
      #enabled?(name)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Checks if a component with the given name (i.e. the same gem group) is enabled. 
- #freeze ⇒ Object private
- 
  
    
      #groups(*groups)  ⇒ Array<String, Symbol> 
    
    
  
  
  
  
  
  
  
  
  
    Merges groups of enabled components with the given ones. 
- #initialize ⇒ self constructor
- 
  
    
      #is_dependency?(name)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Checks if the named component is dependent by another enabled one. 
- 
  
    
      #not(name = nil, any: nil, all: nil)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Matching status. 
- 
  
    
      #not_all(*parts) {|void| ... } ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Matching status. 
- 
  
    
      #not_any(*parts) {|void| ... } ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Matching status. 
- 
  
    
      #on(name = nil, any: nil, all: nil)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Matching status. 
- 
  
    
      #on_all(*parts) {|void| ... } ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Matching status. 
- 
  
    
      #on_any(*parts) {|void| ... } ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Matching status. 
- 
  
    
      #ready!  ⇒ self, void 
    
    
  
  
  
  
  
  
  
  
  
    Once-only set Bootinq to ready state firing the @_on_readycallback.
- #ready? ⇒ Boolean
- 
  
    
      #switch {|switch| ... } 
    
    
  
  
  
  
  
  
  
  
  
    Collector method. 
Constructor Details
#initialize ⇒ self
| 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | # File 'lib/bootinq.rb', line 160 def initialize config = self.class.deserialized_config config.merge!(DEFAULT) { |_, l, r| l.nil? ? r : l } @_orig_value = ENV.fetch(config['env_key']) { config['default'] } @_neg, @_value = FilterNegValue[@_orig_value, config] @_deps = config['deps'] @flags = [] @components = [] config['parts'].each { |flag, name| enable_component(name, flag: flag.to_s) } config['mount'].each { |flag, name| enable_component(name, flag: flag.to_s, as: Mountable) } end | 
Instance Attribute Details
#components ⇒ Object (readonly)
Returns the value of attribute components.
| 157 158 159 | # File 'lib/bootinq.rb', line 157 def components @components end | 
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
| 152 153 154 | # File 'lib/bootinq.rb', line 152 def flags @flags end | 
Class Method Details
.deserialized_config(path: nil) ⇒ Hash
Reads config
| 131 132 133 134 | # File 'lib/bootinq.rb', line 131 def self.deserialized_config(path: nil) bootinq_yaml = File.read(path || ENV.fetch('BOOTINQ_PATH')) psych_safe_load(bootinq_yaml, [Symbol]) end | 
.init(verbose: false, on_ready: ) ⇒ instance .init(verbose: false, &on_ready) ⇒ instance
Sets BOOTINQ_PATH enviroment variable if it is missing & initializes itself
| 114 115 116 117 118 119 120 121 122 123 124 | # File 'lib/bootinq.rb', line 114 def self.init(verbose: false, on_ready: nil, &block) ENV['BOOTINQ_PATH'] ||= File.('../bootinq.yml', caller_locations(2, 1)[0].path) instance on_ready = block.to_proc if on_ready.nil? && block_given? instance.instance_variable_set(:@_on_ready, on_ready.to_proc) if on_ready puts "Bootinq: loading components #{instance.components.join(', ')}" if verbose instance.ready! end | 
.require(*groups, **options, &on_ready)
This method returns an undefined value.
Invokes the init method with the given options and block, then calls Bundler.require with the enabled groups.
| 83 84 85 86 | # File 'lib/bootinq.rb', line 83 def self.require(*groups, **, &on_ready) init(**, &on_ready) Bundler.require(*instance.groups(*groups)) end | 
.setup(*groups, **options) {|instance| ... }
This method returns an undefined value.
Invokes the init method with the given options and block, then calls Bundler.require with the enabled groups.
| 101 102 103 104 | # File 'lib/bootinq.rb', line 101 def self.setup(*groups, **, &on_ready) # :yields: Bootinq.instance init(**, &on_ready) Bundler.setup(*instance.groups(*groups)) end | 
Instance Method Details
#component(name) ⇒ Bootinq::Component Also known as: []
| 224 225 226 | # File 'lib/bootinq.rb', line 224 def component(name) @components[@components.index(name)] end | 
#disabled?(name) ⇒ Boolean
Checks if a component with the given name (i.e. the same gem group) is disabled
| 232 233 234 | # File 'lib/bootinq.rb', line 232 def disabled?(name) !@components.include?(name) end | 
#each_mountable ⇒ Enumerator #each_mountable {|component| ... } ⇒ Enumerator
Enumerates enabled mountable components
| 241 242 243 244 245 246 247 | # File 'lib/bootinq.rb', line 241 def each_mountable return enum_for(:each_mountable) unless block_given? @components.each do |component| yield(component) if component.mountable? end end | 
#enable_component(name, flag:, as: Component) {|name, is_enabled| ... }
This method returns an undefined value.
Enables the given component if it is required by flag or when another enabled component depends it.
| 204 205 206 207 208 209 210 211 212 213 214 | # File 'lib/bootinq.rb', line 204 def enable_component(name, flag:, as: Component) if is_dependency?(name) || @_value.include?(flag) @flags << flag @components << as.new(name) yield(name, true) if block_given? else yield(name, false) if block_given? end nil end | 
#enabled?(name) ⇒ Boolean
Checks if a component with the given name (i.e. the same gem group) is enabled
| 218 219 220 | # File 'lib/bootinq.rb', line 218 def enabled?(name) ALL.include?(name) || @components.include?(name) end | 
#freeze ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
| 405 406 407 408 409 410 411 | # File 'lib/bootinq.rb', line 405 def freeze @_value.freeze @_neg @flags.freeze @components.freeze super end | 
#groups(*groups) ⇒ Array<String, Symbol>
Merges groups of enabled components with the given ones. When loaded with Rails, it passes them to Rails.groups method, otherwise just returns the merged list to use it with Bundler.require.
| 254 255 256 257 258 259 260 261 | # File 'lib/bootinq.rb', line 254 def groups(*groups) @components.each do |component| next if groups.include?(component.group) groups.unshift(component.group) end defined?(Rails) ? Rails.groups(*groups) : groups end | 
#is_dependency?(name) ⇒ Boolean
Checks if the named component is dependent by another enabled one.
| 399 400 401 402 | # File 'lib/bootinq.rb', line 399 def is_dependency?(name) @_deps.key?(name.to_s) && @_value.count(@_deps.dig(name.to_s, 'in').to_s) > 0 end | 
#not(name) {|void| ... } ⇒ Boolean #not(any: ) {|void| ... } ⇒ Boolean #not(all: ) {|void| ... } ⇒ Boolean
Returns matching status.
| 347 348 349 350 351 352 353 354 355 356 | # File 'lib/bootinq.rb', line 347 def not(name = nil, any: nil, all: nil) is_matched = name ? disabled?(name) : any ? not_any(*any) : all ? not_all(*all) : false yield if is_matched is_matched end | 
#not_all(*parts) {|void| ... } ⇒ Boolean
Returns matching status.
| 364 365 366 367 368 | # File 'lib/bootinq.rb', line 364 def not_all(*parts) # :yields: is_matched = parts.reduce(true) { |m, part| m && disabled?(part) } yield if is_matched && block_given? is_matched end | 
#not_any(*parts) {|void| ... } ⇒ Boolean
Returns matching status.
| 376 377 378 379 380 | # File 'lib/bootinq.rb', line 376 def not_any(*parts) # :yields: is_matched = parts.reduce(false) { |m, part| m || disabled?(part) } yield if is_matched && block_given? is_matched end | 
#on(name) {|void| ... } ⇒ Boolean #on(any: ) {|void| ... } ⇒ Boolean #on(all: ) {|void| ... } ⇒ Boolean
Returns matching status.
| 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | # File 'lib/bootinq.rb', line 285 def on(name = nil, any: nil, all: nil) if name && ALL.include?(name) yield return true end is_matched = name ? enabled?(name) : any ? on_any(*any) : all ? on_all(*all) : false yield if is_matched is_matched end | 
#on_all(*parts) {|void| ... } ⇒ Boolean
Returns matching status.
| 307 308 309 310 311 | # File 'lib/bootinq.rb', line 307 def on_all(*parts) # :yields: is_matched = parts.reduce(true) { |m, part| m && enabled?(part) } yield if is_matched && block_given? is_matched end | 
#on_any(*parts) {|void| ... } ⇒ Boolean
Returns matching status.
| 319 320 321 322 323 | # File 'lib/bootinq.rb', line 319 def on_any(*parts) # :yields: is_matched = parts.reduce(false) { |m, part| m || enabled?(part) } yield if is_matched && block_given? is_matched end | 
#ready! ⇒ self, void
Once-only set Bootinq to ready state firing the @_on_ready callback.
| 184 185 186 187 188 189 190 191 192 | # File 'lib/bootinq.rb', line 184 def ready! return if ready? @ready = true if defined?(@_on_ready) Bootinq.class_exec(&@_on_ready) remove_instance_variable :@_on_ready end freeze end | 
#ready? ⇒ Boolean
| 177 178 179 | # File 'lib/bootinq.rb', line 177 def ready? !!@ready end |