【发布时间】:2011-01-16 02:02:03
【问题描述】:
-编辑-
从第一个答案中阅读了 Delegate 方法后,我的问题是,是否可以将两种不同的方法委托给另一个方法。
IE:我目前有:@photo.attachment.file.url、@photo.attachment.height 和 @photo.attachment.width
我希望能够通过@photo.file.url、@photo.file.height、@photo.file.width 访问所有这些。
语法的原因是Attachment是一个使用Paperclip管理文件的模型,Paperclip是生成.file方法的(模型叫做Attachment,模型使用Paperclip的has_attached_file :file)。
-原始问题-
我想知道 Ruby 中的别名方法和属性(我认为这是一个通用的 ruby 问题,尽管我的应用程序是在 Rails 3 中):
我有两个模型:Photo has_one Attachment。
附件具有“高度”和“宽度”属性,以及“文件”方法(来自 Paperclip)。
所以默认情况下我可以像这样访问附件模型的位:
photo.attachment.width # returns width in px
photo.attachment.height # returns height in px
photo.attachment.file # returns file path
photo.attachment.file.url #returns url for the default style variant of the image
photo.attachment.file.url(:style) #returns the url for a given style variant of the image
现在,在我的照片类中,我创建了这个方法:
def file(*args)
attachment.file(*args)
end
所以,现在我可以简单地使用:
photo.file # returns file path
photo.file.url # returns file url (or variant url if you pass a style symbol)
我的问题是,我可以将photo.attachment.file 直接指向photo.file,但我也可以将高度和宽度映射到photo.file,这样,为了保持一致性,我可以访问高度和宽度属性通过photo.file.height 和photo.file.width?
这样的事情有可能吗?如果有,它是什么样子的?
【问题讨论】:
-
为什么不直接将 Photo#height 委托给 Attachment#height?为什么要打扰 photo.file.height?
-
只是为了保持一致性。 Photo.height 很好,但如果所有与文件相关的命令都在 Photo.file 下,那么至少对我来说更适合 Least Surprise 的 Principal。另外,只是好奇是否可以做到。
-
为什么不被接受? Rails 委托方法是这里的答案,对吗?那有什么问题?
-
也许我在这里没有理解某些东西,但我想知道我是否可以将
@photo.attachment.height(其中height是附件模型的一个属性)委托给@photo.file.height,其中file是插件提供的方法。根据给出的答案,我不明白如何做到这一点,我也无法自己推断出来。所以,如果你能回答这个问题,你就会得到赏金:) -
关于您的编辑,与方法相比,ruby 没有单独的属性概念。它们都只是方法。
标签: ruby-on-rails ruby-on-rails-3 alias shortcut