【问题标题】:How do I access options passed to an acts_as plugin (ActiveRecord decorator) from instance methods?如何访问从实例方法传递给acts_as 插件(ActiveRecord 装饰器)的选项?
【发布时间】:2010-02-05 10:45:11
【问题描述】:

目前我将每个选项存储在其自己的类属性中,但是当我需要从实例方法访问传递的选项时,这会导致代码难以阅读。

例如,如果我将列名作为选项传递,我必须使用 self.send(self.class.path_finder_column) 从实例方法中获取列值。

请注意,我已在 class 属性前加上插件名称,以防止名称冲突。

这是一个简单的插件代码示例,它传递了一个选项column,然后从实例方法set_path 访问该选项。可以将 getter/setter 简化为更具可读性吗?

# usage: path_find :column => 'path'

module PathFinder    
  def path_finder(options = {})
    send :include, InstanceMethods

    # Create class attributes for options 
    self.cattr_accessor :path_finder_column
    self.path_finder_column = options[:column]

    module InstanceMethods
      def set_path        
        # setter
        self.send(self.class.path_finder_column + '=', 'some value')
        # getter
        self.send(self.class.path_finder_column)
      end
    end
  end 
end

ActiveRecord::Base.send :extend, PathFinder

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-plugins


    【解决方案1】:

    您可以在运行时生成所有这些方法。

    module PathFinder
      def path_finder(options = {})
    
        # Create class attributes for options 
        self.cattr_accessor :path_finder_options
        self.path_finder_options = options
    
        class_eval <<-RUBY
          def path_finder(value)
            self.#{options[:column]} = value
          end
    
          def path_finder
            self.#{options[:column]}
          end
        RUBY
      end
    end
    
    ActiveRecord::Base.send :extend, PathFinder
    

    除非你需要存储选项,你也可以删除行

    self.cattr_accessor :path_finder_options
    self.path_finder_options = options
    

    请注意,只要您始终使用path_finder 和path_finder=,我的解决方案就不需要setter 和getter。 因此,最短的解决方案是(假设只有 :column 选项,没有其他要求)

    module PathFinder
      def path_finder(options = {})
    
        # here more logic
        # ...
    
        class_eval <<-RUBY
          def path_finder(value)
            self.#{options[:column]} = value
          end
    
          def path_finder
            self.#{options[:column]}
          end
        RUBY
      end
    end
    
    ActiveRecord::Base.send :extend, PathFinder
    

    这种方法类似于acts_as_list和acts_as_tree采用的方法。

    【讨论】:

      【解决方案2】:

      从 cattr_accessor 开始,它为给定的每个符号创建一个类变量。在 ruby​​ 中,类变量的名称以 @@ 为前缀。

      因此您可以使用@@path_finder_column 代替self.class.path_finder_column。

      但是考虑到我接下来要提出的建议,这还没有实际意义。

      在问题中的代码呈现的特定情况下。您定义的组合 getter 和 setter 不符合 ruby​​ 约定。看到您实质上如何使用通用名称为 path_finder_column 生成的访问器重新命名,您可以将其全部简化为一对别名。

      假设组合访问器中有错误(代码应该如何知道是获取还是设置),最终的模块将如下所示:

      module PathFinder    
        def path_finder(options = {})
          send :include, InstanceMethods
      
          # Create class attributes for options 
          self.cattr_accessor :path_finder_column
          self.path_finder_column = options[:column]
          alias :set_path, path_finder_column
          alias :set_path=, "#{path_finder_column}="
        end
      
        module InstanceMethods  
          # other instance methods here.
        end
      end
      

      【讨论】:

        【解决方案3】:

        您可以使用 cattr_accessor 在类级别存储配置值并在所有实例方法中使用。你可以在http://github.com/smsohan/acts_as_permalinkable/blob/master/lib/active_record/acts/permalinkable.rb看到一个例子

        要查看的代码是这样的:

        def acts_as_permalinkable(options = {})
                  send :cattr_accessor, :permalink_options
                  self.permalink_options = { :permalink_method => :name, :permalink_field_name => :permalink, :length => 200 }
                  self.permalink_options.update(options) if options.is_a?(Hash)
        
                  send :include, InstanceMethods
                  send :after_create, :generate_permalink
                end
        

        希望对你有帮助!

        【讨论】:

        • -1:问题中发布的代码已经使用了 cattr_accessor。
        猜你喜欢
        • 2017-11-12
        • 2019-05-24
        • 2021-08-20
        • 2020-05-11
        • 2011-07-05
        • 1970-01-01
        • 2018-06-07
        • 2019-02-03
        相关资源
        最近更新 更多