Class: MockedFixtures::MockFixtures

Inherits:
Fixtures
  • Object
show all
Defined in:
lib/mocked_fixtures/mock_fixtures.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE) ⇒ MockFixtures

Returns a new instance of MockFixtures.



17
18
19
20
21
22
23
24
# File 'lib/mocked_fixtures/mock_fixtures.rb', line 17

def initialize(table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
  @table_name, @fixture_path, @file_filter = table_name, fixture_path, file_filter
  @class_name = class_name || (ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize)
  @table_name = ActiveRecord::Base.table_name_prefix + @table_name + ActiveRecord::Base.table_name_suffix
  @table_name = class_name.table_name if class_name.respond_to?(:table_name)
  @connection = MockedFixtures::MockConnection.new
  read_fixture_files
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/mocked_fixtures/mock_fixtures.rb', line 3

def connection
  @connection
end

Class Method Details

.create_fixtures(fixtures_directory, table_names, class_names = {}) ⇒ Object

Gets loaded fixture files for each table and type casts the values and returns the array of MockFixtures instances. The insert_fixtures method is in the superclass and does all the foxy fixtures work.



8
9
10
11
12
13
14
15
# File 'lib/mocked_fixtures/mock_fixtures.rb', line 8

def self.create_fixtures(fixtures_directory, table_names, class_names = {})
  table_names = Array(table_names).flatten.map { |n| n.to_s }
  fixtures = table_names.map do |table_name|
    fixture = MockedFixtures::MockFixtures.new(File.split(table_name.to_s).last, class_names[table_name.to_sym], File.join(fixtures_directory, table_name.to_s))
    fixture.insert_fixtures
    fixture.each {|label, f| fixture[label] = fixture.connection.loaded_fixtures[fixture.table_name][label] }
  end
end

Instance Method Details

#column_namesObject



37
38
39
# File 'lib/mocked_fixtures/mock_fixtures.rb', line 37

def column_names
  @column_name ||= @connection.columns(@table_name).collect(&:name)
end

#delete_existing_fixturesObject



41
42
43
# File 'lib/mocked_fixtures/mock_fixtures.rb', line 41

def delete_existing_fixtures
  # override to do nothing (NOP) as we are not using the database, so nothing needs deleting
end

#eachObject



50
51
52
53
54
55
56
# File 'lib/mocked_fixtures/mock_fixtures.rb', line 50

def each
  original_each do |label, fixture|
    connection.current_fixture_label = label
    yield label, fixture
    connection.current_fixture_label = nil
  end    
end

#has_primary_key_column?Boolean

overrides method to prevent database hit

Returns:

  • (Boolean)


27
28
29
# File 'lib/mocked_fixtures/mock_fixtures.rb', line 27

def has_primary_key_column?
  @has_primary_key_column ||= !primary_key_name.nil?
end

#original_eachObject

The each method is aliased to allow the fixture label to be captured during iteration in superclass methods, particularly the insert_fixtures method. The label is required for the insert_fixture connection method to be able store the final fixture hash and reference it by its label for later.



49
# File 'lib/mocked_fixtures/mock_fixtures.rb', line 49

alias :original_each :each

#primary_key_nameObject

overrides method to prevent database hit and uses dummy connection which gets primary key from schema.rb



33
34
35
# File 'lib/mocked_fixtures/mock_fixtures.rb', line 33

def primary_key_name
  @primary_key_name ||= @connection.schema[@table_name.to_s][:primary_key]
end