Class: Fixture
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/active_record/fixtures.rb
Overview
Defined Under Namespace
Classes: FixtureError, FormatError
Instance Method Summary
collapse
Constructor Details
#initialize(fixture, class_name) ⇒ Fixture
Returns a new instance of Fixture.
381
382
383
384
385
386
387
388
389
390
391
392
|
# File 'lib/active_record/fixtures.rb', line 381
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} during creation of #{class_name} fixture"
end
@class_name = class_name
end
|
Instance Method Details
#[](key) ⇒ Object
398
399
400
|
# File 'lib/active_record/fixtures.rb', line 398
def [](key)
@fixture[key]
end
|
#each ⇒ Object
394
395
396
|
# File 'lib/active_record/fixtures.rb', line 394
def each
@fixture.each { |item| yield item }
end
|
#find ⇒ Object
421
422
423
424
425
426
427
428
|
# File 'lib/active_record/fixtures.rb', line 421
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_list ⇒ Object
406
407
408
409
|
# File 'lib/active_record/fixtures.rb', line 406
def key_list
columns = @fixture.keys.collect{ |column_name| ActiveRecord::Base.connection.quote_column_name(column_name) }
columns.join(", ")
end
|
#to_hash ⇒ Object
402
403
404
|
# File 'lib/active_record/fixtures.rb', line 402
def to_hash
@fixture
end
|
#value_list ⇒ Object
411
412
413
414
415
416
417
418
419
|
# File 'lib/active_record/fixtures.rb', line 411
def value_list
klass = @class_name.constantize rescue nil
list = @fixture.inject([]) do |fixtures, (key, value)|
col = klass.columns_hash[key] if klass.kind_of?(ActiveRecord::Base)
fixtures << ActiveRecord::Base.connection.quote(value, col).gsub('[^\]\\n', "\n").gsub('[^\]\\r', "\r")
end
list * ', '
end
|