Permalink
Browse files

autoinit.rb: remove use of temporary method

  • Loading branch information...
1 parent 81c2a28 commit 041acc5b69483d7c6416252d008a43fce8a97a2a @quix committed Aug 11, 2009
Showing with 10 additions and 36 deletions.
  1. +10 −10 lib/quix/ext/class/autoinit.rb
  2. +0 −26 lib/quix/temp_method.rb
@@ -1,8 +1,5 @@
-require 'quix/temp_method'
-
class Class
-
#
# autoinit is found in Gavin Sinclair's extensions package. The
# following is my own implementation which does not use eval().
@@ -11,12 +8,11 @@ class Class
# package.
#
def autoinit(*names, &block)
- # TODO: jettison 1.8.6; use |&block|
-
if block and not [-1, 0].include?(block.arity)
raise ArgumentError, "autoinit block must have no arguments"
end
+ # TODO: jettison 1.8.6; use |*values, &init_block|
define_method :initialize do |*values|
unless values.size == names.size
raise ArgumentError,
@@ -26,14 +22,16 @@ def autoinit(*names, &block)
instance_variable_set("@#{name}", value)
}
if block
- Quix::TempMethod.call_temp_method(self, :__autoinit, &block)
+ (class << self ; self ; end).class_eval do
+ define_method :__autoinit, &block
+ end
+ __autoinit
end
end
end
def autoinit_options(*keys, &block)
- # TODO: jettison 1.8.6; use |&block|
-
+ # TODO: jettison 1.8.6; use |*args, &init_block|
define_method :initialize do |*args|
options = args.last.is_a?(Hash) ? args.pop : Hash.new
expected_args_size = (
@@ -56,8 +54,10 @@ def autoinit_options(*keys, &block)
instance_variable_set("@#{key}", value)
}
if block
- Quix::TempMethod.
- call_temp_method(self, :__autoinit_options, *args, &block)
+ (class << self ; self ; end).class_eval do
+ define_method :__autoinit_options, &block
+ end
+ __autoinit_options(*args)
end
end
end
View
@@ -1,26 +0,0 @@
-
-require 'quix/symbol_generator'
-
-module Quix
- module TempMethod
- module_function
-
- def with_temp_method(instance, method_prefix, method_block)
- method_name = "#{method_prefix}[#{SymbolGenerator.gensym}]"
- (class << instance ; self ; end).class_eval do
- define_method(method_name, &method_block)
- begin
- yield method_name
- ensure
- remove_method(method_name)
- end
- end
- end
-
- def call_temp_method(instance, method_prefix, *args, &method_block)
- with_temp_method(instance, method_prefix, method_block) { |method_name|
- instance.send(method_name, *args)
- }
- end
- end
-end

0 comments on commit 041acc5

Please sign in to comment.