title: How to define helper methods for manipulating the Puppet catalogue object
body: <p>I have a bunch of repeated code that manipulates the Puppet catalogue object (notice the repeated <code>content =</code> lines):</p>
  
  <pre><code>require 'nokogiri'
  
  describe 'role::jenkins' do
    before(:each) do
      @jobs = catalogue.resource_keys.select{|k,v| k == 'Jenkins::Jobs'}.map{|k,v| v}
    end
  
    it 'Jenkins jobs should be valid XML' do
      @jobs.each do |j|
        content = catalogue.resource('file', "/tmp/#{j}.xml").send(:parameters)[:content]
        result = Nokogiri::XML(content).errors.empty?
        if ! result
          puts "    Job #{j} does NOT have valid XML"
        end
        expect(result).to be true
      end
    end 
  
    it "XML should contain a variables.json snippet that is valid JSON" do
      @jobs.each do |j|
        content = catalogue.resource('file', "/tmp/#{j}.xml").send(:parameters)[:content]
        if content.match(/cat &amp;lt;&amp;lt; EOF &amp;gt; #{json_file}.*?EOF/m)
          json_snippet = content.match(/#{json_file}(.*?)EOF/m)[1]
          expect { JSON.parse(json_snippet) }.to_not raise_error
        end
      end
    end
  end
  </code></pre>
  
  <p>As can be seen, I have moved a long query into a <code>before(:each)</code> block and saved it in an instance variable. That makes it available in the <code>it</code> blocks.</p>
  
  <p>What I do not understand is how I could define a method for the <code>content =</code> lines, like:</p>
  
  <pre><code>def content(file_name)
    catalogue.resource('file', file_name).send(:parameters)[:content]
  end
  </code></pre>
  
  <p>If I knew how to do that I could clean up this code considerably. What I cannot figure out is where I could place this <code>def</code> block, if it is even possible to do this.</p>