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.



344
345
346
347
# File 'lib/active_record/fixtures.rb', line 344

def initialize(fixture, class_name)
  @fixture = fixture.is_a?(Hash) ? fixture : read_fixture_file(fixture)
  @class_name = class_name
end

Instance Method Details

#[](key) ⇒ Object



353
354
355
# File 'lib/active_record/fixtures.rb', line 353

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

#eachObject



349
350
351
# File 'lib/active_record/fixtures.rb', line 349

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

#findObject



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

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

#key_listObject



361
362
363
364
# File 'lib/active_record/fixtures.rb', line 361

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

#to_hashObject



357
358
359
# File 'lib/active_record/fixtures.rb', line 357

def to_hash
  @fixture
end

#value_listObject



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

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