Class: Yuuki::Caller
- Inherits:
-
Object
- Object
- Yuuki::Caller
- Defined in:
- lib/yuuki/caller.rb
Direct Known Subclasses
Class Method Summary collapse
-
.require_dir(require_dir, recursive: false) ⇒ Object
requires all the rb files in the given directory.
Instance Method Summary collapse
-
#add(*instances) ⇒ Object
adds instances to yuuki.
-
#alive? ⇒ Boolean
(also: #running?)
returns whether any thread is alive.
-
#ignore_tag_error(enabled: true) ⇒ Object
set whether to ignore “tag not associated” error.
-
#initialize(*instances, use_yoshinon: true) ⇒ Caller
constructor
A new instance of Caller.
-
#join ⇒ Object
joins all runnning threads.
-
#run(**args, &block) ⇒ Object
runs all methods.
-
#run_method(klass, method_sig, **args, &block) ⇒ Object
runs the specific method.
-
#run_select(proc_select, **args, &block) ⇒ Object
runs all selected methods.
-
#run_tag(*tags, **args, &block) ⇒ Object
runs all methods with the specified tags.
-
#runners ⇒ Array<[Method, Hash<Symbol, Object>]>
returns runners.
-
#tags ⇒ Object
returns all tags defined as Set.
Constructor Details
#initialize(*instances, use_yoshinon: true) ⇒ Caller
Returns a new instance of Caller.
17 18 19 20 21 22 |
# File 'lib/yuuki/caller.rb', line 17 def initialize(*instances, use_yoshinon: true) @instances = Set.new @threads = [] add(*instances) @use_yoshinon = use_yoshinon end |
Class Method Details
.require_dir(require_dir, recursive: false) ⇒ Object
requires all the rb files in the given directory
12 13 14 |
# File 'lib/yuuki/caller.rb', line 12 def self.require_dir(require_dir, recursive: false) Dir.glob(recursive ? "#{require_dir}/**/*.rb" : "#{require_dir}/*.rb").sort.each {|file| require file} end |
Instance Method Details
#add(*instances) ⇒ Object
adds instances to yuuki
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/yuuki/caller.rb', line 26 def add(*instances) instances.each do |instance| # create instance if class is given if instance.is_a?(Class) klass = instance # check the klass is extended unless klass.singleton_class.include?(Yuuki::Runner) raise Yuuki::Error, 'Runner instance must be extend Yuuki::Runner' end instance = instance.allocate instance.instance_variable_set(:@yuuki, self) instance.send(:initialize) else # check the klass is extended unless instance.class.singleton_class.include?(Yuuki::Runner) raise Yuuki::Error, 'Runner instance must be extend Yuuki::Runner' end # add @yuuki to the instance instance.instance_variable_set(:@yuuki, self) end # regist @instances << instance end end |
#alive? ⇒ Boolean Also known as: running?
returns whether any thread is alive
128 129 130 131 |
# File 'lib/yuuki/caller.rb', line 128 def alive? @threads.select!(&:alive?) !@threads.empty? end |
#ignore_tag_error(enabled: true) ⇒ Object
set whether to ignore “tag not associated” error
89 90 91 |
# File 'lib/yuuki/caller.rb', line 89 def ignore_tag_error(enabled: true) @ignore_tag_error = enabled end |
#join ⇒ Object
joins all runnning threads
121 122 123 124 |
# File 'lib/yuuki/caller.rb', line 121 def join @threads.each(&:join) @threads.select!(&:alive?) end |
#run(**args, &block) ⇒ Object
runs all methods
77 78 79 |
# File 'lib/yuuki/caller.rb', line 77 def run(**args, &block) run_internal(runners, args, &block) end |
#run_method(klass, method_sig, **args, &block) ⇒ Object
runs the specific method
111 112 113 114 115 116 117 118 |
# File 'lib/yuuki/caller.rb', line 111 def run_method(klass, method_sig, **args, &block) select_proc = proc do |method, | flag_klass = klass ? method.receiver.instance_of?(klass) : true flag_method = method_sig ? method.name == method_sig : true flag_klass && flag_method end run_select(select_proc, **args, &block) end |
#run_select(proc_select, **args, &block) ⇒ Object
runs all selected methods
84 85 86 |
# File 'lib/yuuki/caller.rb', line 84 def run_select(proc_select, **args, &block) run_internal(runners.select(&proc_select), args, &block) end |
#run_tag(*tags, **args, &block) ⇒ Object
runs all methods with the specified tags
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/yuuki/caller.rb', line 96 def run_tag(*, **args, &block) t = self. .each do |e| next if t.include?(e) raise Yuuki::Error, "tag `#{e}` is not associated" unless @ignore_tag_error warn "Yuuki Warning: tag `#{e}` is not associated" end run_select(proc {|_method, | [:tags]&.intersect?()}, **args, &block) end |
#runners ⇒ Array<[Method, Hash<Symbol, Object>]>
returns runners
56 57 58 59 60 61 62 |
# File 'lib/yuuki/caller.rb', line 56 def runners list = @instances.flat_map do |instance| methods = instance.class.instance_variable_get(:@yuuki_methods) methods.select {|_sig, | [:enabled]}.map {|sig, | [instance.method(sig), ]} end list.sort_by {|_method, | -([:priority] || 0)} end |
#tags ⇒ Object
returns all tags defined as Set
65 66 67 68 69 70 71 72 73 |
# File 'lib/yuuki/caller.rb', line 65 def = @instances.flat_map do |instance| methods = instance.class.instance_variable_get(:@yuuki_methods) methods.select {|_sig, | [:enabled]}.flat_map {|_sig, | [:tags]} end ret = Set.new .each {|e| ret += e if e} ret end |