Class: Yuuki::Caller

Inherits:
Object
  • Object
show all
Defined in:
lib/yuuki/caller.rb

Direct Known Subclasses

PeriodicCaller

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*instances, use_yoshinon: true) ⇒ Caller

Returns a new instance of Caller.

Parameters:

  • instances (Object)


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

Parameters:

  • require_dir (String)

    directory

  • recursive (Boolean) (defaults to: false)


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

Parameters:

  • instances (Object)


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

Returns:

  • (Boolean)


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

#joinObject

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

Parameters:

  • args (Object)

    arguments



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

Parameters:

  • klass (Class, nil)
  • method_sig (Symbol, nil)

    method name

  • args (Object)

    arguments



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, _meta|
    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

Parameters:

  • proc_select (Proc)
  • args (Object)

    arguments



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

Parameters:

  • tags (Symbol)
  • args (Object)

    arguments



96
97
98
99
100
101
102
103
104
105
# File 'lib/yuuki/caller.rb', line 96

def run_tag(*tags, **args, &block)
  t = self.tags
  tags.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, meta| meta[:tags]&.intersect?(tags)}, **args, &block)
end

#runnersArray<[Method, Hash<Symbol, Object>]>

returns runners

Returns:

  • (Array<[Method, Hash<Symbol, Object>]>)


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, meta| meta[:enabled]}.map {|sig, meta| [instance.method(sig), meta]}
  end
  list.sort_by {|_method, meta| -(meta[:priority] || 0)}
end

#tagsObject

returns all tags defined as Set



65
66
67
68
69
70
71
72
73
# File 'lib/yuuki/caller.rb', line 65

def tags
  tags = @instances.flat_map do |instance|
    methods = instance.class.instance_variable_get(:@yuuki_methods)
    methods.select {|_sig, meta| meta[:enabled]}.flat_map {|_sig, meta| meta[:tags]}
  end
  ret = Set.new
  tags.each {|e| ret += e if e}
  ret
end