我正在嘗試寫一個小型的Ruby DSL,並且偶然發現了一個不便之處。現在我的「DSL」的代碼是https://gist.github.com/0379b07f516f4f322204和我的執行代碼(.html.erb文件中)是:如何編寫與塊的環境不是隔離的DSL?
html_table_for @users do |t, user|
t.field :username, link_to(user.username, :action => 'show', :id => user.id)
t.field :email
t.field :roles, user.roles.map(&:code).join(', ')
end
的實現代碼看起來並不很像,因爲t.field
而不是簡單地field
的DSL。通過用instance_exec(@block)
代替DSL代碼(第34行)中的@block.call
,我幾乎可以得到我想要的,但是我失去了所有的ActionView優點(即link_to
)。
有沒有辦法在塊中提供我的小DSL類的實例方法,同時保持Rails中包含的ActionPack的輔助方法的可用性?