Class: Fixture

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_record/fixtures.rb

Overview

:nodoc:

Defined Under Namespace

Classes: FixtureError, FormatError

Instance Method Summary collapse

Constructor Details

#initialize(fixture, class_name) ⇒ Fixture

Returns a new instance of Fixture.



364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/active_record/fixtures.rb', line 364

def initialize(fixture, class_name)
  case fixture
    when Hash, YAML::Omap
      @fixture = fixture
    when String
      @fixture = read_fixture_file(fixture)
    else
      raise ArgumentError, "Bad fixture argument #{fixture.inspect}"
  end

  @class_name = class_name
end

Instance Method Details

#[](key) ⇒ Object



381
382
383
# File 'lib/active_record/fixtures.rb', line 381

def [](key)
  @fixture[key]
end

#eachObject



377
378
379
# File 'lib/active_record/fixtures.rb', line 377

def each
  @fixture.each { |item| yield item }
end

#findObject



398
399
400
401
402
403
404
405
# File 'lib/active_record/fixtures.rb', line 398

def find
  klass = @class_name.is_a?(Class) ? @class_name : Object.const_get(@class_name) rescue nil
  if klass
    klass.find(self[klass.primary_key])
  else
    raise FixtureClassNotFound, "The class #{@class_name.inspect} was not found."
  end
end

#key_listObject



389
390
391
392
# File 'lib/active_record/fixtures.rb', line 389

def key_list
  columns = @fixture.keys.collect{ |column_name| ActiveRecord::Base.connection.quote_column_name(column_name) }
  columns.join(", ")
end

#to_hashObject



385
386
387
# File 'lib/active_record/fixtures.rb', line 385

def to_hash
  @fixture
end

#value_listObject



394
395
396
# File 'lib/active_record/fixtures.rb', line 394

def value_list
  @fixture.values.map { |v| ActiveRecord::Base.connection.quote(v).gsub('\\n', "\n").gsub('\\r', "\r") }.join(", ")
end