expedite 0.0.2 → 0.1.1

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.
@@ -1,99 +0,0 @@
1
-
2
- module Expedite
3
- class Variant
4
- ##
5
- # Name of the parent variant. This allows you to create variants from
6
- # an existing variant.
7
- # Defaults to nil.
8
- attr_accessor :parent
9
-
10
- ##
11
- # If set to true, variant will be restarted automatically if it is killed.
12
- # Defaults to false.
13
- attr_accessor :keep_alive
14
-
15
- ##
16
- # [parent] Name of parent variant.
17
- # [after_fork] Block is executed when variant is first preloaded.
18
- def initialize(parent: nil, keep_alive: false, &after_fork)
19
- @parent = parent
20
- @keep_alive = keep_alive
21
- @after_fork_proc = after_fork
22
- end
23
-
24
- ##
25
- # Called when variant if first preloaded. This version calls the after_fork
26
- # block provided in the initializer.
27
- def after_fork(variant)
28
- @after_fork_proc&.call(variant)
29
- end
30
- end
31
-
32
- class Variants
33
- Registration = Struct.new(:matcher, :variant) do
34
- def match?(name)
35
- File.fnmatch?(matcher, name)
36
- end
37
- end
38
-
39
- def self.current
40
- @current ||= Variants.new
41
- end
42
-
43
- ##
44
- # Retrieves the specified variant
45
- def self.lookup(variant)
46
- self.current.lookup(variant)
47
- end
48
-
49
- ##
50
- # Registers a variant. Variants are matched in the
51
- # order they are registered.
52
- #
53
- # [matcher] Wildcard to match a name against.
54
- # [named_options] Variant options.
55
- # [after_fork] Optional block that is called when
56
- # variant is preloaded.
57
- #
58
- # = Example
59
- # Expedite::Variants.register('base' do |name|
60
- # puts "Base #{name} started"
61
- # end
62
- # Expedite::Variants.register('development/abc', parent: 'base') do |name|
63
- # puts "Variant #{name} started"
64
- # end
65
- def self.register(matcher, **named_options, &after_fork)
66
- self.current.register(matcher, **named_options, &after_fork)
67
- end
68
-
69
- ##
70
- # Resets registrations to default
71
- def self.reset
72
- self.current.reset
73
- end
74
-
75
- def initialize
76
- @registrations = []
77
- end
78
-
79
- def lookup(variant)
80
- ret = @registrations.find do |r|
81
- r.match?(variant)
82
- end
83
- raise NotImplementedError, "Variant #{variant.inspect} not found" if ret.nil?
84
- ret.variant
85
- end
86
-
87
- def register(matcher, **named_options, &after_fork)
88
- @registrations << Registration.new(
89
- matcher,
90
- Variant.new(**named_options, &after_fork)
91
- )
92
- end
93
-
94
- def reset
95
- @registrations = []
96
- nil
97
- end
98
- end
99
- end