class Expedite::Variants

Constants

Registration

Public Class Methods

current() click to toggle source
# File lib/expedite/variants.rb, line 39
def self.current
  @current ||= Variants.new
end
lookup(variant) click to toggle source

Retrieves the specified variant

# File lib/expedite/variants.rb, line 45
def self.lookup(variant)
  self.current.lookup(variant)
end
new() click to toggle source
# File lib/expedite/variants.rb, line 75
def initialize
  @registrations = []
end
register(matcher, **named_options, &after_fork) click to toggle source

Registers a variant. Variants are matched in the order they are registered.

matcher

Wildcard to match a name against.

named_options

Variant options.

after_fork

Optional block that is called when variant is preloaded.

Example

Expedite::Variants.register('base' do |name|
  puts "Base #{name} started"
end
Expedite::Variants.register('development/abc', parent: 'base') do |name|
  puts "Variant #{name} started"
end
# File lib/expedite/variants.rb, line 65
def self.register(matcher, **named_options, &after_fork)
  self.current.register(matcher, **named_options, &after_fork)
end
reset() click to toggle source

Resets registrations to default

# File lib/expedite/variants.rb, line 71
def self.reset
  self.current.reset
end

Public Instance Methods

lookup(variant) click to toggle source
# File lib/expedite/variants.rb, line 79
def lookup(variant)
  ret = @registrations.find do |r|
    r.match?(variant)
  end
  raise NotImplementedError, "Variant #{variant.inspect} not found" if ret.nil?
  ret.variant
end
register(matcher, **named_options, &after_fork) click to toggle source
# File lib/expedite/variants.rb, line 87
def register(matcher, **named_options, &after_fork)
  @registrations << Registration.new(
    matcher,
    Variant.new(**named_options, &after_fork)
  )
end
reset() click to toggle source
# File lib/expedite/variants.rb, line 94
def reset
  @registrations = []
  nil
end