lib/everyday-cli-utils/override.rb in everyday-cli-utils-1.8.2 vs lib/everyday-cli-utils/override.rb in everyday-cli-utils-1.8.3

- old
+ new

@@ -13,10 +13,25 @@ end def get(obj) OverridesInstance.new(@overrides, obj) end + + class << self + def register_override(s, s2, method_name, &block) + s.class_eval { + MethodOverrides.store_true_override(s, s2, method_name) + s.create_method(method_name.to_sym, &block) + } + end + + def store_true_override(s, s2, method_name) + original_method = s.instance_method(method_name.to_sym) + s2.true_overrides ||= MethodOverrides.new + s2.true_overrides.store_override(method_name.to_sym, original_method) + end + end end class OverridesInstance def initialize(overrides, obj, ind = 0) @overrides = overrides @@ -27,24 +42,31 @@ def [](ind) OverridesInstance.new(@overrides, @obj, @ind - ind) end def call_override(method_symbol, *args, &block) - if @overrides.has_key?(method_symbol.to_sym) - overrides = @overrides[method_symbol.to_sym] - ind = overrides.count + (@ind - 1) - ovin = Thread.current["overrides_ind_#{@obj.__id__}"] || 0 - ind += ovin - Thread.current["overrides_ind_#{@obj.__id__}"] = @ind + ovin - 1 - rv = overrides[ind].bind(@obj).call(*args, &block) - Thread.current["overrides_ind_#{@obj.__id__}"] = ovin - rv - else - @obj.send(method_symbol.to_sym, *args, &block) - end + @overrides.has_key?(method_symbol.to_sym) ? -> { + overrides, ind, ovin = get_overrides_and_inds(method_symbol) + call_override_at_index(overrides, ind, ovin, args, &block) + }.call : @obj.send(method_symbol.to_sym, *args, &block) end + def call_override_at_index(overrides, ind, ovin, args, &block) + Thread.current["overrides_ind_#{@obj.__id__}"] = @ind + ovin - 1 + rv = overrides[ind].bind(@obj).call(*args, &block) + Thread.current["overrides_ind_#{@obj.__id__}"] = ovin + rv + end + + def get_overrides_and_inds(method_symbol) + overrides = @overrides[method_symbol.to_sym] + ind = overrides.count + (@ind - 1) + ovin = Thread.current["overrides_ind_#{@obj.__id__}"] || 0 + ind += ovin + return overrides, ind, ovin + end + def method_missing(symbol, *args, &block) call_override(symbol, *args, &block) end end @@ -64,19 +86,14 @@ def true_overrides=(overrides) @overrides = overrides end def override(method_name, &block) - s2 = self - class << self + s = class << self self - end.class_eval { - original_method = instance_method(method_name.to_sym) - s2.true_overrides ||= MethodOverrides.new - s2.true_overrides.store_override(method_name.to_sym, original_method) - self.create_method(method_name.to_sym, &block) - } + end + MethodOverrides.register_override(s, self, method_name, &block) end class << self def class_overrides(s) @overrides && @overrides.get(s) @@ -89,14 +106,9 @@ def true_overrides=(overrides) @overrides = overrides end def override(method_name, &block) - self.class_eval { - original_method = instance_method(method_name.to_sym) - self.true_overrides ||= MethodOverrides.new - self.true_overrides.store_override(method_name.to_sym, original_method) - self.create_method(method_name.to_sym, &block) - } + MethodOverrides.register_override(self, self, method_name, &block) end end end \ No newline at end of file