動態創建的模塊,我用一個模式嘗試,我想反饋:紅寶石
module Concerns
def AuthenticatedS3Concern(options)
AuthenticatedS3ConcernHelper.go(options)
end
module_function :AuthenticatedS3Concern
module AuthenticatedS3ConcernHelper
def self.go(options = {:attribute => :photo})
@@auth_attr = options[:attribute] # the photo clip reference
@@auth_attr_url = "#{@@auth_attr}_authenticated_url" # set this to do a one time download
Module.new do
def self.included(base)
base.send :include, AuthenticatedS3ConcernHelper::InstanceMethods
end
class_eval %(
def #{@@auth_attr}_authenticated_url(time_limit = 7.days)
authenticated_url_for('#{@@auth_attr}', time_limit)
end
)
end
end
module InstanceMethods
def authenticated_url_for(attached_file, time_limit)
AWS::S3::S3Object.url_for(self.send(attached_file).path('original'), self.send(attached_file).bucket_name, :expires_in => time_limit)
end
end
end
end
哪位能像這樣被使用:
require 'concerns/authenticated_s3_concern'
require 'concerns/remote_file_concern'
class Attachment
include Concerns.AuthenticatedS3Concern(:attribute => :attachment)
end
我很好奇,如果這是一個好方法或一個壞方法或什麼。有沒有更好的方法來完成這種可變定義模塊的東西?
感謝