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.



353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/active_record/fixtures.rb', line 353

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



370
371
372
# File 'lib/active_record/fixtures.rb', line 370

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

#eachObject



366
367
368
# File 'lib/active_record/fixtures.rb', line 366

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

#findObject



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

def find
  if Object.const_defined?(@class_name)
    klass = Object.const_get(@class_name)
    klass.find(self[klass.primary_key])
  end
end

#key_listObject



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

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

#to_hashObject



374
375
376
# File 'lib/active_record/fixtures.rb', line 374

def to_hash
  @fixture
end

#value_listObject



383
384
385
# File 'lib/active_record/fixtures.rb', line 383

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