有人可以向我解释这个 ruby 语法是如何工作的吗?
Can someone explain to me how this ruby syntax works?
以下是 OpenProject 开源项目的一段代码。我不知道这个 ruby 代码在做什么,并且阅读 ruby 文档也没有帮助。
初始化程序中的代码正在调用 manage 方法,但我不确定传入的参数是什么。
使用调试器,当我在管理方法中查看 'item' 的内容时,它只是说,:block.
有人可以解释或推荐一些文档来解释如何调用 manage 吗?
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
传递给 manage 的参数是 :blocks
和一个块。
yield
只是让出对作为参数传入的块的控制。
yield
正在使用 homescreen[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? } }
)
以下是 OpenProject 开源项目的一段代码。我不知道这个 ruby 代码在做什么,并且阅读 ruby 文档也没有帮助。
初始化程序中的代码正在调用 manage 方法,但我不确定传入的参数是什么。
使用调试器,当我在管理方法中查看 'item' 的内容时,它只是说,:block.
有人可以解释或推荐一些文档来解释如何调用 manage 吗?
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
传递给 manage 的参数是 :blocks
和一个块。
yield
只是让出对作为参数传入的块的控制。
yield
正在使用 homescreen[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? } }
)