Class: Fixture
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/active_record/fixtures.rb
Overview
Defined Under Namespace
Classes: FixtureError, FormatError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(fixture, class_name) ⇒ Fixture
Returns a new instance of Fixture.
758
759
760
761
762
763
764
765
766
767
768
769
|
# File 'lib/active_record/fixtures.rb', line 758
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 Attribute Details
#class_name ⇒ Object
Returns the value of attribute class_name.
756
757
758
|
# File 'lib/active_record/fixtures.rb', line 756
def class_name
@class_name
end
|
Instance Method Details
#[](key) ⇒ Object
775
776
777
|
# File 'lib/active_record/fixtures.rb', line 775
def [](key)
@fixture[key]
end
|
#each ⇒ Object
771
772
773
|
# File 'lib/active_record/fixtures.rb', line 771
def each
@fixture.each { |item| yield item }
end
|
#find ⇒ Object
798
799
800
801
802
803
804
805
|
# File 'lib/active_record/fixtures.rb', line 798
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
783
784
785
786
|
# File 'lib/active_record/fixtures.rb', line 783
def key_list
columns = @fixture.keys.collect{ |column_name| ActiveRecord::Base.connection.quote_column_name(column_name) }
columns.join(", ")
end
|
#to_hash ⇒ Object
779
780
781
|
# File 'lib/active_record/fixtures.rb', line 779
def to_hash
@fixture
end
|
#value_list ⇒ Object
788
789
790
791
792
793
794
795
796
|
# File 'lib/active_record/fixtures.rb', line 788
def value_list
klass = @class_name.constantize rescue nil
list = @fixture.inject([]) do |fixtures, (key, value)|
col = klass.columns_hash[key] if klass.respond_to?(:ancestors) && klass.ancestors.include?(ActiveRecord::Base)
fixtures << ActiveRecord::Base.connection.quote(value, col).gsub('[^\]\\n', "\n").gsub('[^\]\\r', "\r")
end
list * ', '
end
|