[Groonga-commit] droonga/droonga-engine at e8e0e23 [master] Make Deferrable module to accept multiple "on_ready" callbacks

Back to archive index

Kouhei Sutou kou****@clear*****
Tue Apr 21 12:44:29 JST 2015


> +    def on_ready=(callback)
> +      @on_ready_callbacks ||= []
> +      if callback
> +        @on_ready_callbacks << callback
> +      else
> +        @on_ready_callbacks.clear
> +      end
> +      callback
> +    end

このAPIはやり過ぎじゃないですかねぇ。
たぶん、互換性を維持しようとしてこうなったんだと思いますけど、
字面から想像する動きと実際の動きが違うので後でつらくなると思
います。

通常、

  xxx.on_ready = XXX

という見た目のコードではXXXで「置き換える」という動きを想像
しますが、実際は「追加する」なので、わかりにくくなると思いま
す。

  def on_ready_callbacks
    @on_ready_callbacks ||= []
  end

  private
  def on_ready
    on_ready_callbacks.each do |callback|
      callback.call
    end
  end

にしちゃっていいんじゃないでしょうか。

で、使うときはこう。

  xxx.on_ready_callbacks << lambda do
    # ...
  end

In <e8e0e23ecf1a5336baf2a4bfaac2a79de8e41929 �� jenkins.clear-code.com>
  "[Groonga-commit] droonga/droonga-engine �� e8e0e23 [master] Make Deferrable module to accept multiple "on_ready" callbacks" on Tue, 21 Apr 2015 11:02:52 +0900,
  YUKI Hiroshi <null+groonga �� clear-code.com> wrote:

> YUKI Hiroshi	2015-04-21 11:02:52 +0900 (Tue, 21 Apr 2015)
> 
>   New Revision: e8e0e23ecf1a5336baf2a4bfaac2a79de8e41929
>   https://github.com/droonga/droonga-engine/commit/e8e0e23ecf1a5336baf2a4bfaac2a79de8e41929
> 
>   Message:
>     Make Deferrable module to accept multiple "on_ready" callbacks
> 
>   Modified files:
>     lib/droonga/deferrable.rb
> 
>   Modified: lib/droonga/deferrable.rb (+16 -2)
> ===================================================================
> --- lib/droonga/deferrable.rb    2015-04-21 10:45:00 +0900 (2b0dee4)
> +++ lib/droonga/deferrable.rb    2015-04-21 11:02:52 +0900 (1977c52)
> @@ -15,7 +15,7 @@
>  
>  module Droonga
>    module Deferrable
> -    attr_writer :on_ready, :on_failure
> +    attr_writer :on_failure
>  
>      def wait_until_ready(target)
>        target.on_ready = lambda do
> @@ -23,9 +23,23 @@ module Droonga
>        end
>      end
>  
> +    def on_ready=(callback)
> +      @on_ready_callbacks ||= []
> +      if callback
> +        @on_ready_callbacks << callback
> +      else
> +        @on_ready_callbacks.clear
> +      end
> +      callback
> +    end
> +
>      private
>      def on_ready
> -      @on_ready.call if @on_ready
> +      if @on_ready_callbacks
> +        @on_ready_callbacks.each do |callback|
> +          callback.call
> +        end
> +      end
>      end
>  
>      def on_failure




More information about the Groonga-commit mailing list
Back to archive index