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

Returns a new instance of Fixture.



763
764
765
766
767
768
769
770
771
772
773
774
# File 'lib/active_record/fixtures.rb', line 763

def initialize(fixture, class_name)
  case fixture
    when Hash, YAML::Omap
      @fixture = fixture
    when String
      @fixture = read_fixture_file(fixture)
    else
      raise ArgumentError, "Bad fixture argument #{fixture.inspect} during creation of #{class_name} fixture"
  end

  @class_name = class_name
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



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

def class_name
  @class_name
end

Instance Method Details

#[](key) ⇒ Object



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

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

#eachObject



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

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

#findObject



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

def find
  klass = @class_name.is_a?(Class) ? @class_name : Object.const_get(@class_name) rescue nil
  if klass
    klass.find(self[klass.primary_key])
  else
    raise FixtureClassNotFound, "The class #{@class_name.inspect} was not found."
  end
end

#key_listObject



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

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

#to_hashObject



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

def to_hash
  @fixture
end

#value_listObject



793
794
795
796
797
798
799
800
801
# File 'lib/active_record/fixtures.rb', line 793

def value_list
  klass = @class_name.constantize rescue nil

  list = @fixture.inject([]) do |fixtures, (key, value)|
    col = klass.columns_hash[key] if klass.respond_to?(:ancestors) && klass.ancestors.include?(ActiveRecord::Base)
    fixtures << ActiveRecord::Base.connection.quote(value, col).gsub('[^\]\\n', "\n").gsub('[^\]\\r', "\r")
  end
  list * ', '
end