Class: Fixture

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mongoid_rails_fixtures/mongoid_ext/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 = Mongoid.database) ⇒ Fixture

Returns a new instance of Fixture.



769
770
771
772
773
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 769

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



767
768
769
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 767

def model_class
  @model_class
end

Instance Method Details

#[](key) ⇒ Object



783
784
785
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 783

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

#class_nameObject



775
776
777
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 775

def class_name
  @model_class.name if @model_class
end

#eachObject



779
780
781
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 779

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

#findObject



804
805
806
807
808
809
810
811
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 804

def find
  forced_primary_key = model_class.primary_key ? model_class.primary_key : 'id'
  if model_class
    model_class.find(forced_primary_key)
  else
    raise FixtureClassNotFound, "No class attached to find."
  end
end

#key_listObject



791
792
793
794
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 791

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

#to_hashObject



787
788
789
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 787

def to_hash
  @fixture
end

#value_listObject



796
797
798
799
800
801
802
# File 'lib/mongoid_rails_fixtures/mongoid_ext/fixtures.rb', line 796

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