File: uithreadonly.rb

package info (click to toggle)
mikutter 5.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,780 kB
  • sloc: ruby: 22,912; sh: 186; makefile: 21
file content (34 lines) | stat: -rw-r--r-- 1,099 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# -*- coding: utf-8 -*-

require 'set'

# このモジュールをincludeすると、そのクラスの全てのメソッドは
module UiThreadOnly
  PREFIX = 'qawsedrftgyhujikolp_'.freeze

  def self.included(klass)

    klass.instance_eval{

      class << self
        defined = Set.new
        define_method(:mainthread_only){ |method_name|
          if not(defined.include?(method_name.to_sym)) and not(method_name.to_s.start_with?(UiThreadOnly::PREFIX))
            defined << method_name.to_sym
            new_method = :"#{UiThreadOnly::PREFIX}#{method_name}"
            alias_method(new_method, method_name)
            define_method(method_name) { |*args, &proc|
              raise ThreadError.new("call #{self.class}##{method_name} not at main thread.") if Thread.current != Thread.main
              __send__(new_method, *args, &proc) } end }
      end

      (public_instance_methods - Class.new.public_instance_methods).each{ |method_name|
        mainthread_only method_name
      }

      def method_added(method_name)
        mainthread_only method_name
      end
    }
  end
end