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.



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

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.



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

def model_class
  @model_class
end

Instance Method Details

#[](key) ⇒ Object



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

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

#class_nameObject



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

def class_name
  @model_class.name if @model_class
end

#eachObject



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

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

#findObject



806
807
808
809
810
811
812
# File 'lib/active_record/fixtures.rb', line 806

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



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

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

#to_hashObject



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

def to_hash
  @fixture
end

#value_listObject



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

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