How to make a local variable an instance variable for an ERB template with binding?

1

Below I exemplify my need in a code where I need to redefine variables such as @dispute = dispute to be instance variables and the template loaded by ERB can access them through binding :

class Remittance::ExportService
  def initialize(remittance)
    @remittance = remittance
    @template = 'app/views/remittances/letter.text.erb'
  end

  def letters
    tempfile = Tempfile.new(['post', '.txt'])

    @remittance.disputes.each_with_index do |dispute, index|
      @dispute = dispute
      @index = index

      dispute.users.each do |user|
        @user = user

        tempfile.write ERB.new(File.read(@template)).result binding
      end
    end

    tempfile.rewind

    @remittance.post = tempfile
    @remittance.save
  end
end

Is there any way to automatically make local variables into instance variables using an each or some magic method?

    
asked by anonymous 02.02.2017 / 17:10

0 answers