Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails gem中ActiveRecord的转向扩展_Ruby On Rails_Ruby On Rails 3_Activerecord_Rubygems - Fatal编程技术网

Ruby on rails gem中ActiveRecord的转向扩展

Ruby on rails gem中ActiveRecord的转向扩展,ruby-on-rails,ruby-on-rails-3,activerecord,rubygems,Ruby On Rails,Ruby On Rails 3,Activerecord,Rubygems,此扩展为所有型号的应用程序创建cache\u find方法(我已使用此方法创建) config/active\u record\u extension.rb require 'active_support/concern' module ActiveRecordExtension extend ActiveSupport::Concern # add your instance methods here def flush_find Rails.cache.delete

此扩展为所有型号的应用程序创建
cache\u find
方法(我已使用此方法创建)

config/active\u record\u extension.rb

require 'active_support/concern'

module ActiveRecordExtension

  extend ActiveSupport::Concern

  # add your instance methods here
  def flush_find
    Rails.cache.delete([self.class.name, :cached_find, id])
  end

  included do
    after_commit :flush_find
  end

  module ClassMethods
    def cached_find id
      Rails.cache.fetch([self.name, :cached_find, id]) { self.find(id) }
    end
  end
end

# include the extension
ActiveRecord::Base.send(:include, ActiveRecordExtension)
我将此代码转换为gem并添加到其中

所以我想动态地添加这些方法,比如:

class User << ActiveRecord::Base
  # id, name, email, age...

  cached :find, :find_by_name, :find_by_email
end

class User要动态添加代码,需要破解ActiveRecord::基类。在另一个文件(通常放在lib/core\u ext中)中,可以执行以下操作:

ActiveRecord::Base.class_eval do
  def self.cached(*args)
    args.each do |arg|
      define_method "cached_#{arg.to_s}" do 
        # do whatever you want to do inside cached_xx
      end

      define_method "flush_#{arg.to_s}" do
        # do whatever you want to to inside flush_xx
      end
    end
  end
end
它的基本功能是获取缓存的所有参数(:find,:find_by_name,等等),并定义两个方法(cache_find,cache_find_by_name)和flush_find。。等)


希望这有帮助

要动态添加代码,需要破解ActiveRecord::基类。在另一个文件(通常放在lib/core\u ext中)中,可以执行以下操作:

ActiveRecord::Base.class_eval do
  def self.cached(*args)
    args.each do |arg|
      define_method "cached_#{arg.to_s}" do 
        # do whatever you want to do inside cached_xx
      end

      define_method "flush_#{arg.to_s}" do
        # do whatever you want to to inside flush_xx
      end
    end
  end
end
它的基本功能是获取缓存的所有参数(:find,:find_by_name,等等),并定义两个方法(cache_find,cache_find_by_name)和flush_find。。等)


希望这有帮助

您不必破解ActiveRecord::Base。你可以把Marc Alexandre说的话添加到你的顾虑中,比如:

module ActiveRecordExtension
  extend ActiveSupport::Concern

  ...

  module ClassMethods
    def cached(*args)
      define_method "cached_#{arg.to_s}" do 
        # do whatever you want to do inside cached_xx
      end

      define_method "flush_#{arg.to_s}" do
        # do whatever you want to to inside flush_xx
      end
    end
  end
end

另外,我不会直接在ActiveRecord中自动包含扩展,我认为最好在您将要使用它的模型中显式地包含它。

您不必攻击ActiveRecord::Base。你可以把Marc Alexandre说的话添加到你的顾虑中,比如:

module ActiveRecordExtension
  extend ActiveSupport::Concern

  ...

  module ClassMethods
    def cached(*args)
      define_method "cached_#{arg.to_s}" do 
        # do whatever you want to do inside cached_xx
      end

      define_method "flush_#{arg.to_s}" do
        # do whatever you want to to inside flush_xx
      end
    end
  end
end

另外,我不会直接在ActiveRecord中自动包含扩展,我认为最好在您将要使用它的模型中显式地包含它。

很好。现在,我需要访问
define_method
块中的Rails类。我必须将Rails依赖项添加到
gempec
文件??因为您的gem正在扩展Rails,是的,您应该:)很好。现在,我需要访问
define_method
块中的Rails类。我必须将Rails依赖项添加到
gempec
文件??因为您的gem正在扩展Rails,是的,您应该:)