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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fixture, model_class, connection = ActiveRecord::Base.connection) ⇒ Fixture

Returns a new instance of Fixture.



765
766
767
768
769
# File 'lib/active_record/fixtures.rb', line 765

def initialize(fixture, model_class, connection = ActiveRecord::Base.connection)
  @connection = connection
  @fixture = fixture
  @model_class = model_class.is_a?(Class) ? model_class : model_class.constantize rescue nil
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



763
764
765
# File 'lib/active_record/fixtures.rb', line 763

def model_class
  @model_class
end

Instance Method Details

#[](key) ⇒ Object



779
780
781
# File 'lib/active_record/fixtures.rb', line 779

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

#class_nameObject



771
772
773
# File 'lib/active_record/fixtures.rb', line 771

def class_name
  @model_class.name if @model_class
end

#eachObject



775
776
777
# File 'lib/active_record/fixtures.rb', line 775

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

#findObject



800
801
802
803
804
805
806
# File 'lib/active_record/fixtures.rb', line 800

def find
  if model_class
    model_class.find(self[model_class.primary_key])
  else
    raise FixtureClassNotFound, "No class attached to find."
  end
end

#key_listObject



787
788
789
790
# File 'lib/active_record/fixtures.rb', line 787

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

#to_hashObject



783
784
785
# File 'lib/active_record/fixtures.rb', line 783

def to_hash
  @fixture
end

#value_listObject



792
793
794
795
796
797
798
# File 'lib/active_record/fixtures.rb', line 792

def value_list
  list = @fixture.inject([]) do |fixtures, (key, value)|
    col = model_class.columns_hash[key] if model_class.respond_to?(:ancestors) && model_class.ancestors.include?(ActiveRecord::Base)
    fixtures << @connection.quote(value, col).gsub('[^\]\\n', "\n").gsub('[^\]\\r', "\r")
  end
  list * ', '
end