以下是OpenProject开源项目的代码片段。我对这个ruby代码正在做什么感到茫然,并且通过ruby文档没有帮助。
初始化程序中的代码正在调用manage方法,但我不确定传入的参数是什么。
使用调试器,当我查看' item'的内容时在manage方法中,它只是说:block。
有人向我解释或推荐一些可以解释如何调用管理的文档?
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
答案 0 :(得分:2)
传递给manage的参数是:blocks
和一个块。
yield
,只是控制作为参数传入的块。
yield
调用 homescreen[item]
,其中item等于: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? } }
)