动态创建的模块,我用一个模式尝试,我想反馈:红宝石
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
我很好奇,如果这是一个好方法或一个坏方法或什么。有没有更好的方法来完成这种可变定义模块的东西?
感谢