Class: Fixture

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gems/activerecord-2.2.2/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.



773
774
775
776
# File 'lib/gems/activerecord-2.2.2/lib/active_record/fixtures.rb', line 773

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.



771
772
773
# File 'lib/gems/activerecord-2.2.2/lib/active_record/fixtures.rb', line 771

def model_class
  @model_class
end

Instance Method Details

#[](key) ⇒ Object



786
787
788
# File 'lib/gems/activerecord-2.2.2/lib/active_record/fixtures.rb', line 786

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

#class_nameObject



778
779
780
# File 'lib/gems/activerecord-2.2.2/lib/active_record/fixtures.rb', line 778

def class_name
  @model_class.name if @model_class
end

#eachObject



782
783
784
# File 'lib/gems/activerecord-2.2.2/lib/active_record/fixtures.rb', line 782

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

#findObject



807
808
809
810
811
812
813
# File 'lib/gems/activerecord-2.2.2/lib/active_record/fixtures.rb', line 807

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



794
795
796
797
# File 'lib/gems/activerecord-2.2.2/lib/active_record/fixtures.rb', line 794

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

#to_hashObject



790
791
792
# File 'lib/gems/activerecord-2.2.2/lib/active_record/fixtures.rb', line 790

def to_hash
  @fixture
end

#value_listObject



799
800
801
802
803
804
805
# File 'lib/gems/activerecord-2.2.2/lib/active_record/fixtures.rb', line 799

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