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) ⇒ Fixture

Returns a new instance of Fixture.



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

def initialize(fixture, model_class)
  @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.



761
762
763
# File 'lib/active_record/fixtures.rb', line 761

def model_class
  @model_class
end

Instance Method Details

#[](key) ⇒ Object



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

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

#class_nameObject



768
769
770
# File 'lib/active_record/fixtures.rb', line 768

def class_name
  @model_class.name if @model_class
end

#eachObject



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

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

#findObject



797
798
799
800
801
802
803
# File 'lib/active_record/fixtures.rb', line 797

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



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

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

#to_hashObject



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

def to_hash
  @fixture
end

#value_listObject



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

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 << ActiveRecord::Base.connection.quote(value, col).gsub('[^\]\\n', "\n").gsub('[^\]\\r', "\r")
  end
  list * ', '
end