2

以下は、OpenProject オープン ソース プロジェクトのコード スニペットです。この Ruby コードが何をしているのか途方に暮れており、Ruby のドキュメントを読んでも役に立ちませんでした。

管理メソッドは初期化子のコードによって呼び出されていますが、渡された引数が何であるかわかりません。

デバッガーを使用して、manage メソッドの「item」の内容を見ると、単に :block と表示されています。

CAmanage がどのように呼び出されているかを説明しているドキュメントを誰かが説明してくれたり、紹介してくれたりしませんか?

require 'open_project/homescreen'

  OpenProject::Homescreen.manage :blocks do |blocks|
  blocks.push(
    { partial: 'welcome',
      if: Proc.new { Setting.welcome_on_homescreen? && !Setting.welcome_text.empty? } },
    { partial: 'projects' },
    { partial: 'users',
      if: Proc.new { User.current.admin? } },
    { partial: 'my_account',
      if: Proc.new { User.current.logged? } },
    { partial: 'news',
      if: Proc.new { !@news.empty? } },
    { partial: 'community' },
    { partial: 'administration',
      if: Proc.new { User.current.admin? } }
  )
end


module OpenProject
  module Homescreen
    class << self
      ##
      # Access a defined item on the homescreen
      # By default, this will likely be :blocks and :links,
      # however plugins may define their own blocks and
      # render them in the call_hook.
      def [](item)
        homescreen[item]
      end

      ##
      # Manage the given content for this item,
      # yielding it.
      def manage(item, default = [])
        homescreen[item] ||= default
        yield homescreen[item]
      end

      private

      def homescreen
        @content ||= {}
      end
    end
  end
end

open_project/homescreen.rb

4

1 に答える 1

2

manage に渡される引数は:blocks、 とブロックです。

,yieldは、引数として渡されたブロックに制御を渡すだけです。

yieldhomescreen[item]item equalsで呼び出されています:blocks

したがって、ブロックyieldに渡すだけです。homescreen[:blocks]

コードはこれを行うことになります:

homescreen[:blocks].push (
    { partial: 'welcome',
      if: Proc.new { Setting.welcome_on_homescreen? && !Setting.welcome_text.empty? } },
    { partial: 'projects' },
    { partial: 'users',
      if: Proc.new { User.current.admin? } },
    { partial: 'my_account',
      if: Proc.new { User.current.logged? } },
    { partial: 'news',
      if: Proc.new { !@news.empty? } },
    { partial: 'community' },
    { partial: 'administration',
      if: Proc.new { User.current.admin? } }
    )
于 2015-10-29T23:03:45.630 に答える