class DataMapper::Fixtures::File

Public Class Methods

new(file) click to toggle source
# File lib/dm-fixtures/fixture_set/file.rb, line 23
def initialize(file)
  @file = file
  @rows = nil
end
open(file) { |x| ... } click to toggle source

Open a fixture file named file. When called with a block, the block is called with the filehandle and the filehandle is automatically closed when the block finishes.

# File lib/dm-fixtures/fixture_set/file.rb, line 18
def self.open(file)
  x = new file
  block_given? ? yield(x) : x
end

Public Instance Methods

each(&block) click to toggle source
# File lib/dm-fixtures/fixture_set/file.rb, line 28
def each(&block)
  rows.each(&block)
end

Private Instance Methods

render(content) click to toggle source
# File lib/dm-fixtures/fixture_set/file.rb, line 50
def render(content)
  ERB.new(content).result
end
rows() click to toggle source
# File lib/dm-fixtures/fixture_set/file.rb, line 39
def rows
  return @rows if @rows

  begin
    data = YAML.load(render(IO.read(@file)))
  rescue *RESCUE_ERRORS => error
    raise Fixture::FormatError, "a YAML error occurred parsing #{@file}. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Please have a look at http://www.yaml.org/faq.html\nThe exact error was:\n #{error.class}: #{error}", error.backtrace
  end
  @rows = data ? validate(data).to_a : []
end
validate(data) click to toggle source

Validate our unmarshalled data.

# File lib/dm-fixtures/fixture_set/file.rb, line 55
def validate(data)
  unless Hash === data || YAML::Omap === data
    raise Fixture::FormatError, 'fixture is not a hash'
  end

  raise Fixture::FormatError unless data.all? { |name, row| Hash === row }
  data
end