How do I access parameters passed to act_as plugin (ActiveRecord decorator) from instance methods?

At the moment, I save each option in my own class attribute, but this leads to hard-to-read code when I need to access the passed parameters from instance methods.

For example, if I pass the column name as a parameter, I should use self.send(self.class.path_finder_column)to get the column value from the instance method.

Notice that I prefix the class attribute with the name of my plugin to prevent name clashes.

Here is a simple example of a plugin code that is passed a parameter column, which is then accessed from an instance method set_path. Could simplifications for getters / setters be more readable?

# 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
+3
3

.

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=. , ( : 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.

+1

cattr_accessor . @@.

, @@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
+1

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
source

Source: https://habr.com/ru/post/1731531/


All Articles