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.



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

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.



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

def model_class
  @model_class
end

Instance Method Details

#[](key) ⇒ Object



792
793
794
# File 'lib/active_record/fixtures.rb', line 792

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

#class_nameObject



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

def class_name
  @model_class.name if @model_class
end

#eachObject



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

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

#findObject



811
812
813
814
815
816
817
# File 'lib/active_record/fixtures.rb', line 811

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



800
801
802
# File 'lib/active_record/fixtures.rb', line 800

def key_list
  @fixture.keys.map { |column_name| @connection.quote_column_name(column_name) }.join(', ')
end

#to_hashObject



796
797
798
# File 'lib/active_record/fixtures.rb', line 796

def to_hash
  @fixture
end

#value_listObject



804
805
806
807
808
809
# File 'lib/active_record/fixtures.rb', line 804

def value_list
  cols = (model_class && model_class < ActiveRecord::Base) ? model_class.columns_hash : {}
  @fixture.map do |key, value|
    @connection.quote(value, cols[key]).gsub('[^\]\\n', "\n").gsub('[^\]\\r', "\r")
  end.join(', ')
end