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.



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

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.



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

def model_class
  @model_class
end

Instance Method Details

#[](key) ⇒ Object



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

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

#class_nameObject



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

def class_name
  @model_class.name if @model_class
end

#eachObject



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

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

#findObject



823
824
825
826
827
828
829
# File 'lib/active_record/fixtures.rb', line 823

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



812
813
814
# File 'lib/active_record/fixtures.rb', line 812

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

#to_hashObject



808
809
810
# File 'lib/active_record/fixtures.rb', line 808

def to_hash
  @fixture
end

#value_listObject



816
817
818
819
820
821
# File 'lib/active_record/fixtures.rb', line 816

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