yuuki 0.1.2 → 0.1.5

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
2
  SHA256:
3
- metadata.gz: 1d3d8f9e2aa09bcc0a19661a400c20d5efbc522d5f116e7cc8fd0ce6cf9a7212
4
- data.tar.gz: 5b1724b25b7ca140eda2e70eabf39c1a748aae79464199f2bcba06687afe6767
3
+ metadata.gz: 867eb018041b573177e2245e00709723f1d7d3cfce7e92d627683b1c53027f11
4
+ data.tar.gz: 1625aaef62934efa0d47e5d5adf024947a5380f46046f3f6e10b5cab9248efb8
5
5
  SHA512:
6
- metadata.gz: 2de44a625ca13e0d1e290996b7e02ec30725ed5c463ce33894490a797eea4603242aa9e735479a8ef01613030815c6cd3f9bdb544760ca397d84fc52059875df
7
- data.tar.gz: 351c404e1a6ab8939cb93f944da4d3ba4742a2e147a7e1ae59490a4f43b51a20f1c9df1df5aa78184131dc1b8fdf7c99d84ead60de1a3622efc6017e9477b89f
6
+ metadata.gz: cffebda7874bf6f338f14aa8c6df460e867f090023180e60d04b24ebac0089cfe0e98e953daf88a397527c6cd1c6b5991ae661345057e02cfa2fb0fb21115537
7
+ data.tar.gz: 66eaed2d5695b4174c55163a466a33013960ebefbc806394d94f69f1704dea1ad04334c999c498b54986ee12c100693030ef5ee02a3470940ca34d776e24e3f9
data/.DS_Store ADDED
Binary file
data/lib/yuuki/caller.rb CHANGED
@@ -5,37 +5,46 @@ require 'yuuki/runner'
5
5
 
6
6
  module Yuuki
7
7
  class Caller
8
+ # requires all the rb files in the given directory
9
+ # @param [String] require_dir directory
10
+ # @param [Boolean] recursive
8
11
  def self.require_dir(require_dir, recursive: false)
9
12
  Dir.glob(recursive ? "#{require_dir}/**/*.rb" : "#{require_dir}/*.rb"){|file| require file}
10
13
  end
11
14
 
15
+ # @param [Object] instances
12
16
  def initialize(*instances)
13
17
  @instances = Set.new
14
18
  @threads = []
15
19
  add(*instances)
16
20
  end
17
21
 
22
+ # adds instances to yuuki
23
+ # @param [Object] instances
18
24
  def add(*instances)
19
25
  instances.each do |instance|
20
26
  # create instance if class is given
21
27
  if instance.is_a?(Class)
22
28
  klass = instance
23
- instance = instance.new
29
+ # check the klass is extended
30
+ raise Yuuki::Error, 'Runner instance must be extend Yuuki::Runner' unless klass.singleton_class.include?(Yuuki::Runner)
31
+ instance = instance.allocate
32
+ instance.instance_variable_set(:@yuuki, self)
33
+ instance.send(:initialize)
24
34
  else
25
- klass = instance.class
35
+ # check the klass is extended
36
+ raise Yuuki::Error, 'Runner instance must be extend Yuuki::Runner' unless instance.class.singleton_class.include?(Yuuki::Runner)
37
+ # add @yuuki to the instance
38
+ instance.instance_variable_set(:@yuuki, self)
26
39
  end
27
40
 
28
- # check the klass is extended
29
- raise Yuuki::Error, 'Runner instance must be extend Yuuki::Runner' unless klass.singleton_class.include?(Yuuki::Runner)
30
-
31
- # add @yuuki to the instance
32
- instance.instance_variable_set(:@yuuki, self)
33
-
34
41
  # regist
35
42
  @instances << instance
36
43
  end
37
44
  end
38
45
 
46
+ # returns runners
47
+ # @return [Array<[Method, Hash<Symbol, Object>]>]
39
48
  def runners
40
49
  list = @instances.flat_map do |instance|
41
50
  methods = instance.class.instance_variable_get(:@yuuki_methods)
@@ -44,18 +53,41 @@ module Yuuki
44
53
  list.sort_by{|_method, meta| -(meta[:priority] || 0)}
45
54
  end
46
55
 
56
+ # returns all tags defined as Set
57
+ def tags
58
+ tags = @instances.flat_map do |instance|
59
+ methods = instance.class.instance_variable_get(:@yuuki_methods)
60
+ methods.select{|_sig, meta| meta[:enabled]}.flat_map{|_sig, meta| meta[:tags]}
61
+ end
62
+ ret = Set.new
63
+ tags.each{|e| ret += e if e}
64
+ ret
65
+ end
66
+
67
+ # runs all methods
68
+ # @param [Object] args arguments
47
69
  def run(**args, &block)
48
70
  run_internal(runners, args, &block)
49
71
  end
50
72
 
73
+ # runs all selected methods
74
+ # @param [Proc] proc_select
75
+ # @param [Object] args arguments
51
76
  def run_select(proc_select, **args, &block)
52
77
  run_internal(runners.select(&proc_select), args, &block)
53
78
  end
54
79
 
80
+ # runs all methods with the specified tags
81
+ # @param [Symbol] tags
82
+ # @param [Object] args arguments
55
83
  def run_tag(*tags, **args, &block)
56
84
  run_select(proc{|_method, meta| meta[:tags]&.intersect?(tags)}, **args, &block)
57
85
  end
58
86
 
87
+ # runs the specific method
88
+ # @param [Class, nil] klass
89
+ # @param [Symbol, nil] method_sig method name
90
+ # @param [Object] args arguments
59
91
  def run_method(klass, method_sig, **args, &block)
60
92
  select_proc = proc do |method, _meta|
61
93
  flag_klass = klass ? method.receiver.instance_of?(klass) : true
@@ -65,11 +97,14 @@ module Yuuki
65
97
  run_select(select_proc, **args, &block)
66
98
  end
67
99
 
100
+ # joins all runnning threads
68
101
  def join
69
102
  @threads.each(&:join)
70
103
  @threads.select!(&:alive?)
71
104
  end
72
105
 
106
+ # returns whether any thread is alive
107
+ # @return [Boolean]
73
108
  def alive?
74
109
  @threads.select!(&:alive?)
75
110
  !@threads.empty?
@@ -5,23 +5,33 @@ require 'yuuki/runner'
5
5
 
6
6
  module Yuuki
7
7
  module Runner
8
- # set interval to the method
9
- def periodic(method, interval)
8
+ # sets interval to the method
9
+ # @param [Symbol] methods method names
10
+ # @param [Integer] interval
11
+ def periodic(*methods, interval)
10
12
  @yuuki_methods ||= {}
11
- @yuuki_methods[method] ||= {}
12
- @yuuki_methods[method][:periodic] = interval
13
+ methods.each do |method|
14
+ @yuuki_methods[method] ||= {}
15
+ @yuuki_methods[method][:periodic] = interval
16
+ end
13
17
  end
14
18
 
15
- # set whether the method run at the first time
16
- def first_run(method, enabled: true)
19
+ # sets whether the method run at the first time
20
+ # @param [Symbol] methods method names
21
+ # @param [Boolean] enabled
22
+ def first_run(*methods, enabled: true)
17
23
  @yuuki_methods ||= {}
18
- @yuuki_methods[method] ||= {}
19
- @yuuki_methods[method][:first_run] = enabled
24
+ methods.each do |method|
25
+ @yuuki_methods[method] ||= {}
26
+ @yuuki_methods[method][:first_run] = enabled
27
+ end
20
28
  end
21
29
  end
22
30
  end
23
31
 
24
32
  module Yuuki
33
+ # @attr_reader [Boolean] first_run
34
+ # @attr_reader [Float] current_time
25
35
  class PeriodicCaller < Caller
26
36
  attr_reader :first_run, :current_time
27
37
 
@@ -30,10 +40,16 @@ module Yuuki
30
40
  @first_run = true
31
41
  end
32
42
 
43
+ # sets error callback
44
+ # @yield [error]
45
+ # @yieldparam [Exception] error
33
46
  def on_error(&block)
34
47
  @on_error = block
35
48
  end
36
49
 
50
+ # runs the periodic caller
51
+ # @param [Numeric] gmtoff GMT Offset
52
+ # @param [Object] args arguments
37
53
  def run(gmtoff = Time.now.gmtoff, **args, &block)
38
54
  last_time = nil
39
55
  loop do
data/lib/yuuki/runner.rb CHANGED
@@ -4,7 +4,8 @@ require 'set'
4
4
 
5
5
  module Yuuki
6
6
  module Runner
7
- # add method
7
+ # adds methods to yuuki
8
+ # @param [Symbol] methods method names
8
9
  def add(*methods)
9
10
  @yuuki_methods ||= {}
10
11
  methods.each do |method|
@@ -13,7 +14,8 @@ module Yuuki
13
14
  end
14
15
  end
15
16
 
16
- # delete method
17
+ # deletes methods from yuuki
18
+ # @param [Symbol] methods method names
17
19
  def delete(*methods)
18
20
  @yuuki_methods ||= {}
19
21
  methods.each do |method|
@@ -22,7 +24,9 @@ module Yuuki
22
24
  end
23
25
  end
24
26
 
25
- # add tags to the method
27
+ # adds tags to the method
28
+ # @param [Symbol] method method name
29
+ # @param [Symbol] tags tag names
26
30
  def tag(method, *tags)
27
31
  @yuuki_methods ||= {}
28
32
  @yuuki_methods[method] ||= {}
@@ -30,7 +34,9 @@ module Yuuki
30
34
  @yuuki_methods[method][:tags].merge(tags)
31
35
  end
32
36
 
33
- # enable threading to the method
37
+ # enables threading for the methods
38
+ # @param [Symbol] methods method names
39
+ # @param [Boolean] enabled
34
40
  def thread(*methods, enabled: true)
35
41
  @yuuki_methods ||= {}
36
42
  methods.each do |method|
@@ -39,7 +45,9 @@ module Yuuki
39
45
  end
40
46
  end
41
47
 
42
- # set priority to the method
48
+ # sets priority to the method
49
+ # @param [Symbol] methods method names
50
+ # @param [Numeric] priority
43
51
  def priority(*methods, priority)
44
52
  @yuuki_methods ||= {}
45
53
  methods.each do |method|
data/lib/yuuki/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yuuki
4
- VERSION = '0.1.2'
4
+ VERSION = '0.1.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yuuki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ishotihadus
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-22 00:00:00.000000000 Z
11
+ date: 2022-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,6 +59,7 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - ".DS_Store"
62
63
  - ".gitignore"
63
64
  - Gemfile
64
65
  - LICENSE.txt