Sha256: b796bb2a2db165d21f1816f4313aa1272fe0c8fbb02ee4fd2ab569a64abff217

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

# ERB parsing credit:
# http://stackoverflow.com/questions/8954706/render-an-erb-template-with-values-from-a-hash/9734736#9734736

require 'erb'
require 'ostruct'

class Fixture
  attr_accessor :file, :locals

  def initialize(file, locals = {})
    @file   = fixture(file)
    @locals = locals
  end

  def fixture_path
    File.expand_path("../../fixtures", __FILE__)
  end

  def fixture(file)
    File.new(File.join(fixture_path, "/", file))
  end

  def to_s
    if File.extname(file) == ".erb"
      ERB.new(template_file_content).result(OpenStruct.new(locals).instance_eval { binding }).to_s
    else
      template_file_content.to_s
    end
  end

  def to_json
    if File.extname(file) == ".erb"
      rendered_file = ERB.new(template_file_content).result(OpenStruct.new(locals).instance_eval { binding })
      JSON.parse(rendered_file)
    else
      JSON.parse(template_file_content)
    end
  end

  def to_json_hashie
    json = self.to_json
    if json.is_a? Array
      json.map {|json_object| Hashie::Mash.new json_object }
    else
      Hashie::Mash.new json
    end
  end

  private

  def template_file_content
    @file_content ||= file.read
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
git_reflow-0.8.9 spec/support/fixtures.rb
git_reflow-0.8.8 spec/support/fixtures.rb
git_reflow-0.8.7 spec/support/fixtures.rb