| 1 |
|
| 2 |
|
| 3 |
require 'set' |
| 4 |
|
| 5 |
すると、そのクラスの全てのメソッドは |
| 6 |
module UiThreadOnly |
| 7 |
PREFIX = 'qawsedrftgyhujikolp_'.freeze |
| 8 |
|
| 9 |
def self.included(klass) |
| 10 |
|
| 11 |
klass.instance_eval{ |
| 12 |
|
| 13 |
class << self |
| 14 |
defined = Set.new |
| 15 |
define_method(:mainthread_only){ |method_name| |
| 16 |
if not(defined.include?(method_name.to_sym)) and not(method_name.to_s.start_with?(UiThreadOnly::PREFIX)) |
| 17 |
defined << method_name.to_sym |
| 18 |
new_method = :"#{UiThreadOnly::PREFIX}#{method_name}" |
| 19 |
alias_method(new_method, method_name) |
| 20 |
define_method(method_name) { |*args, &proc| |
| 21 |
raise ThreadError.new("call #{self.class}##{method_name} not at main thread.") if Thread.current != Thread.main |
| 22 |
__send__(new_method, *args, &proc) } end } |
| 23 |
end |
| 24 |
|
| 25 |
(public_instance_methods - Class.new.public_instance_methods).each{ |method_name| |
| 26 |
mainthread_only method_name |
| 27 |
} |
| 28 |
|
| 29 |
def method_added(method_name) |
| 30 |
mainthread_only method_name |
| 31 |
end |
| 32 |
} |
| 33 |
end |
| 34 |
end |