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.



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

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.



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

def model_class
  @model_class
end

Instance Method Details

#[](key) ⇒ Object



798
799
800
# File 'lib/active_record/fixtures.rb', line 798

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

#class_nameObject



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

def class_name
  @model_class.name if @model_class
end

#eachObject



794
795
796
# File 'lib/active_record/fixtures.rb', line 794

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

#findObject



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

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



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

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

#to_hashObject



802
803
804
# File 'lib/active_record/fixtures.rb', line 802

def to_hash
  @fixture
end

#value_listObject



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

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