yuuki 0.1.2 → 0.1.3

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: 49851d0c15f259e4c54568ff173a45b9ae9316e25c4120fd3d12926f2560fcfb
4
+ data.tar.gz: f17a0f2ec8d4de59becb43c9f6e4de08de3b38534583250f1899da127576188c
5
5
  SHA512:
6
- metadata.gz: 2de44a625ca13e0d1e290996b7e02ec30725ed5c463ce33894490a797eea4603242aa9e735479a8ef01613030815c6cd3f9bdb544760ca397d84fc52059875df
7
- data.tar.gz: 351c404e1a6ab8939cb93f944da4d3ba4742a2e147a7e1ae59490a4f43b51a20f1c9df1df5aa78184131dc1b8fdf7c99d84ead60de1a3622efc6017e9477b89f
6
+ metadata.gz: a2ef440f3446c5d737abfa611f81731b06905189013c3220560a00a9f0e23d6422180a699934a9161711390a49d2e5bbf61f5178bd4316c6c1bcc2e1bad33889
7
+ data.tar.gz: 895d9874e7455840667ea75a90bb02c669d5b384a88b0412ee3bd4be58082465489317b483d2b8e28de81d6d2224f5819bb6af5b34fb1bb9a907b1db053e4164
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,30 @@ module Yuuki
44
53
  list.sort_by{|_method, meta| -(meta[:priority] || 0)}
45
54
  end
46
55
 
56
+ # runs all methods
57
+ # @param [Object] args arguments
47
58
  def run(**args, &block)
48
59
  run_internal(runners, args, &block)
49
60
  end
50
61
 
62
+ # runs all selected methods
63
+ # @param [Proc] proc_select
64
+ # @param [Object] args arguments
51
65
  def run_select(proc_select, **args, &block)
52
66
  run_internal(runners.select(&proc_select), args, &block)
53
67
  end
54
68
 
69
+ # runs all methods with the specified tags
70
+ # @param [Symbol] tags
71
+ # @param [Object] args arguments
55
72
  def run_tag(*tags, **args, &block)
56
73
  run_select(proc{|_method, meta| meta[:tags]&.intersect?(tags)}, **args, &block)
57
74
  end
58
75
 
76
+ # runs the specific method
77
+ # @param [Class, nil] klass
78
+ # @param [Symbol, nil] method_sig method name
79
+ # @param [Object] args arguments
59
80
  def run_method(klass, method_sig, **args, &block)
60
81
  select_proc = proc do |method, _meta|
61
82
  flag_klass = klass ? method.receiver.instance_of?(klass) : true
@@ -65,11 +86,14 @@ module Yuuki
65
86
  run_select(select_proc, **args, &block)
66
87
  end
67
88
 
89
+ # joins all runnning threads
68
90
  def join
69
91
  @threads.each(&:join)
70
92
  @threads.select!(&:alive?)
71
93
  end
72
94
 
95
+ # returns whether any thread is alive
96
+ # @return [Boolean]
73
97
  def alive?
74
98
  @threads.select!(&:alive?)
75
99
  !@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.3'
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.3
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-04-30 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