提交 83dd6e1e 编写于 作者: A Aaron Patterson

favor composition over inheritence. use AS::OrderedHash rather than omap

上级 b17d8d72
......@@ -12,15 +12,7 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/logger'
if RUBY_VERSION < '1.9'
module YAML #:nodoc:
class Omap #:nodoc:
def keys; map { |k, v| k } end
def values; map { |k, v| v } end
end
end
end
require 'active_support/ordered_hash'
if defined? ActiveRecord
class FixtureClassNotFound < ActiveRecord::ActiveRecordError #:nodoc:
......@@ -452,7 +444,7 @@ class FixturesFileNotFound < StandardError; end
#
# Any fixture labeled "DEFAULTS" is safely ignored.
class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash)
class Fixtures
MAX_ID = 2 ** 30 - 1
DEFAULT_FILTER_RE = /\.ya?ml$/
......@@ -552,9 +544,10 @@ def self.identify(label)
Zlib.crc32(label.to_s) % MAX_ID
end
attr_reader :table_name, :name
attr_reader :table_name, :name, :fixtures
def initialize(connection, table_name, class_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
@fixtures = ActiveSupport::OrderedHash.new
@connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
@name = table_name # preserve fixture base name
@class_name = class_name ||
......@@ -565,6 +558,22 @@ def initialize(connection, table_name, class_name, fixture_path, file_filter = D
read_fixture_files
end
def [](x)
fixtures[x]
end
def []=(k,v)
fixtures[k] = v
end
def each(&block)
fixtures.each(&block)
end
def size
fixtures.size
end
def delete_existing_fixtures
@connection.delete "DELETE FROM #{@connection.quote_table_name(table_name)}", 'Fixture Delete'
end
......@@ -574,18 +583,14 @@ def insert_fixtures
now = now.to_s(:db)
# allow a standard key to be used for doing defaults in YAML
if is_a?(Hash)
delete('DEFAULTS')
else
delete(assoc('DEFAULTS'))
end
fixtures.delete('DEFAULTS')
# track any join tables we need to insert later
habtm_fixtures = Hash.new do |h, habtm|
h[habtm] = HabtmFixtures.new(@connection, habtm.options[:join_table], nil, nil)
end
each do |label, fixture|
fixtures.each do |label, fixture|
row = fixture.to_hash
if model_class && model_class < ActiveRecord::Base
......@@ -725,7 +730,7 @@ def read_yaml_fixture_files
raise Fixture::FormatError, "Bad data for #{@class_name} fixture named #{name} (nil)"
end
self[name] = Fixture.new(data, model_class, @connection)
fixtures[name] = Fixture.new(data, model_class, @connection)
end
end
end
......@@ -738,7 +743,7 @@ def read_csv_fixture_files
reader.each do |row|
data = {}
row.each_with_index { |cell, j| data[header[j].to_s.strip] = cell.to_s.strip }
self["#{@class_name.to_s.underscore}_#{i+=1}"] = Fixture.new(data, model_class, @connection)
fixtures["#{@class_name.to_s.underscore}_#{i+=1}"] = Fixture.new(data, model_class, @connection)
end
end
......
......@@ -245,7 +245,7 @@ def test_resets_to_min_pk_with_default_pk_and_sequence
def test_create_fixtures_resets_sequences_when_not_cached
@instances.each do |instance|
max_id = create_fixtures(instance.class.table_name).inject(0) do |_max_id, (_, fixture)|
max_id = create_fixtures(instance.class.table_name).fixtures.inject(0) do |_max_id, (_, fixture)|
fixture_id = fixture['id'].to_i
fixture_id > _max_id ? fixture_id : _max_id
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册