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.
367
368
369
370
371
372
373
374
375
376
377
378
|
# File 'lib/active_record/fixtures.rb', line 367
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
384
385
386
|
# File 'lib/active_record/fixtures.rb', line 384
def [](key)
@fixture[key]
end
|
#each ⇒ Object
380
381
382
|
# File 'lib/active_record/fixtures.rb', line 380
def each
@fixture.each { |item| yield item }
end
|
#find ⇒ Object
407
408
409
410
411
412
413
414
|
# File 'lib/active_record/fixtures.rb', line 407
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
392
393
394
395
|
# File 'lib/active_record/fixtures.rb', line 392
def key_list
columns = @fixture.keys.collect{ |column_name| ActiveRecord::Base.connection.quote_column_name(column_name) }
columns.join(", ")
end
|
#to_hash ⇒ Object
388
389
390
|
# File 'lib/active_record/fixtures.rb', line 388
def to_hash
@fixture
end
|
#value_list ⇒ Object
397
398
399
400
401
402
403
404
405
|
# File 'lib/active_record/fixtures.rb', line 397
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
|