【问题标题】:In ruby, how do I know what a memoist method is doing?在 ruby​​ 中,我怎么知道 memoist 方法在做什么?
【发布时间】:2015-03-30 21:22:36
【问题描述】:

我现在正在基于健康数据标准库 (https://github.com/projectcypress/health-data-standards.git) 进行开发。在文件 lib/health-data-standards/export/helper/scooped_view_helper.rb (https://github.com/projectcypress/health-data-standards/blob/master/lib/health-data-standards/export/helper/scooped_view_helper.rb) 第 90 行中,调用了 patient.entries_for_oid。但是方法没有定义。

在 Pry 环境下,我尝试了“edit patient.entries_for_oid”。编辑器在第 166 行打开 memoist/lib/memoist.rb (https://github.com/matthewrudy/memoist/blob/master/lib/memoist.rb)。在那里我看到了这些代码:

          module_eval <<-EOS, __FILE__, __LINE__ + 1
        def #{method_name}(*args)
          reload = Memoist.extract_reload!(method(#{unmemoized_method.inspect}), args)

          skip_cache = reload || !(instance_variable_defined?(#{memoized_ivar.inspect}) && #{memoized_ivar} && #{memoized_ivar}.has_key?(args))
          set_cache = skip_cache && !frozen?

          if skip_cache
            value = #{unmemoized_method}(*args)
          else
            value = #{memoized_ivar}[args]
          end

          if set_cache
            #{memoized_ivar} ||= {}
            #{memoized_ivar}[args] = value
          end

          value
        end
      EOS
    end

但这没有意义,patient.entries_for_oid 真正做了什么。

有人知道吗?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    方法是defined 上的Record 模型类。这样一个类的实例被传递给你的助手中的entries_for_data_criteria 方法。

    您不会简单地看到实际的方法源,因为作者选择使用 Memoist gem,它可用于包装任意方法以扩展其行为(在本例中为缓存,或更准确地说是记忆化)。

    【讨论】:

    • 那么我该如何调试那个“方法”呢?它现在过滤掉了我的一条重要记录。
    猜你喜欢
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 2017-04-11
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多