Ruby.Metaprogramming. class_eval - PullRequest
0 голосов
/ 12 марта 2012

Кажется, в моем коде ошибка. Однако я просто не могу это выяснить.

class Class
def attr_accessor_with_history(attr_name)
  attr_name = attr_name.to_s

  attr_reader attr_name
  attr_writer attr_name

  attr_reader attr_name + "_history"
  class_eval %Q{
   @#{attr_name}_history=[1,2,3]
  }

end
end

class Foo
 attr_accessor_with_history :bar
end

f = Foo.new
f.bar = 1
f.bar = 2
puts f.bar_history.to_s

Я ожидаю, что он вернет массив [1,2,3]. Однако он ничего не возвращает.

Ответы [ 5 ]

5 голосов
/ 12 марта 2012

Решение:

class Class
  def attr_accessor_with_history(attr_name)
    ivar         = "@#{attr_name}"
    history_meth = "#{attr_name}_history"
    history_ivar = "@#{history_meth}"

    define_method(attr_name) { instance_variable_get ivar }

    define_method "#{attr_name}=" do |value|
      instance_variable_set ivar, value
      instance_variable_set history_ivar, send(history_meth) << value
    end

    define_method history_meth do
      value = instance_variable_get(history_ivar) || []
      value.dup
    end
  end
end

Тесты:

describe 'Class#attr_accessor_with_history' do
  let(:klass)     { Class.new { attr_accessor_with_history :bar } }
  let(:instance)  { instance = klass.new }

  it 'acs as attr_accessor' do
    instance.bar.should be_nil
    instance.bar = 1
    instance.bar.should == 1
    instance.bar = 2
    instance.bar.should == 2
  end

  it 'remembers history of setting' do
    instance.bar_history.should == []
    instance.bar = 1
    instance.bar_history.should == [1]
    instance.bar = 2
    instance.bar_history.should == [1, 2]
  end

  it 'is not affected by mutating the history array' do
    instance.bar_history << 1
    instance.bar_history.should == []
    instance.bar = 1
    instance.bar_history << 2
    instance.bar_history.should == [1]
  end
end
5 голосов
/ 12 марта 2012

Вы не должны открывать Class для добавления новых методов.Вот для чего нужны модули.

module History
  def attr_accessor_with_history(attr_name)
    attr_name = attr_name.to_s

    attr_accessor attr_name

    class_eval %Q{
      def #{attr_name}_history
        [1, 2, 3]
      end
    }

  end
end

class Foo
  extend History
  attr_accessor_with_history :bar
end

f = Foo.new
f.bar = 1
f.bar = 2
puts f.bar_history.inspect
# [1, 2, 3]

А вот код, который вы, вероятно, хотели написать (судя по названиям).

module History
  def attr_accessor_with_history(attr_name)
    attr_name = attr_name.to_s

    class_eval %Q{
      def #{attr_name}
        @#{attr_name}
      end

      def #{attr_name}= val
        @#{attr_name}_history ||= []
        @#{attr_name}_history << #{attr_name}

        @#{attr_name} = val
      end

      def #{attr_name}_history
        @#{attr_name}_history
      end
    }

  end
end

class Foo
  extend History
  attr_accessor_with_history :bar
end

f = Foo.new
f.bar = 1
f.bar = 2
puts f.bar_history.inspect
# [nil, 1]
2 голосов
/ 12 марта 2012

@ Ответ Серджио Туленцева работает, но он продвигает проблематичную практику использования строки eval, которая в целом чревата угрозами безопасности и другими неожиданностями, когда входные данные не соответствуют вашим ожиданиям.Например, что происходит с версией Серджио, если кто-то вызывает (нет, не пытайтесь это сделать):

attr_accessor_with_history %q{foo; end; system "rm -rf /"; def foo}

Часто можно выполнить мета-программирование ruby ​​более аккуратно, без строки eval.В этом случае, используя простую интерполяцию и define_method замыканий с instance_variable_ [get | set], и отправьте:

module History

  def attr_accessor_with_history(attr_name)
    getter_sym  = :"#{attr_name}"
    setter_sym  = :"#{attr_name}="
    history_sym = :"#{attr_name}_history"
    iv_sym      = :"@#{attr_name}"
    iv_hist     = :"@#{attr_name}_history"

    define_method getter_sym do
      instance_variable_get(iv_sym)
    end

    define_method setter_sym do |val|
      instance_variable_set( iv_hist, [] ) unless send(history_sym)
      send(history_sym).send( :'<<', send(getter_sym) )
      instance_variable_set( iv_sym, val @)
    end

    define_method history_sym do
      instance_variable_get(iv_hist)
    end

  end
end
2 голосов
/ 12 марта 2012

Решение вашей проблемы вы найдете в Sergios answer .Вот объяснение того, что происходит в вашем коде.

С

class_eval %Q{
 @#{attr_name}_history=[1,2,3]
}

вы выполняете

 @bar_history = [1,2,3]

Вы выполняете это на уровне класса, а не на уровне объекта.Переменная @bar_history недоступна в Foo-объекте, но в Foo-классе.

С помощью

puts f.bar_history.to_s

вы получаете доступ к -never на уровне объекта, определенном-attribute @bar_history.

Когда вы определяете читателя на уровне класса, у вас есть доступ к вашей переменной:

class << Foo 
  attr_reader :bar_history
end
p Foo.bar_history  #-> [1, 2, 3]
0 голосов
/ 29 августа 2012

Вот что нужно сделать. Атрибут attr_writer должен быть определен с помощью class_eval вместо этого в Class.

class Class
  def attr_accessor_with_history(attr_name)
    attr_name = attr_name.to_s

    attr_reader attr_name
    #attr_writer attr_name  ## moved into class_eval

    attr_reader attr_name + "_history"

    class_eval %Q{
      def #{attr_name}=(value)
        @#{attr_name}_history=[1,2,3]
      end
    }

end
end
...