Class: ActiveShard::Fixtures::Shard
- Inherits:
-
Object
- Object
- ActiveShard::Fixtures::Shard
- Defined in:
- lib/active_shard/fixtures.rb
Instance Attribute Summary collapse
-
#fixture_names ⇒ Object
readonly
Returns the value of attribute fixture_names.
-
#fixtures ⇒ Object
readonly
Returns the value of attribute fixtures.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#shard ⇒ Object
readonly
Returns the value of attribute shard.
Instance Method Summary collapse
- #connection ⇒ Object
-
#initialize(schema, shard, shard_fixtures_dir, *args) ⇒ Shard
constructor
A new instance of Shard.
- #reset! ⇒ Object
- #setup_fixture_accessors(obj) ⇒ Object
Constructor Details
#initialize(schema, shard, shard_fixtures_dir, *args) ⇒ Shard
Returns a new instance of Shard.
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/active_shard/fixtures.rb', line 86 def initialize(schema, shard, shard_fixtures_dir, *args) @schema = schema @shard = shard @fixtures_dir = shard_fixtures_dir @fixture_names = Dir[File.join(@fixtures_dir, '*.yml')].map {|f| File.basename(f, '.yml') } @fixtures = ::Fixtures.create_fixtures(@fixtures_dir, @fixture_names, *args) do connection end end |
Instance Attribute Details
#fixture_names ⇒ Object (readonly)
Returns the value of attribute fixture_names.
83 84 85 |
# File 'lib/active_shard/fixtures.rb', line 83 def fixture_names @fixture_names end |
#fixtures ⇒ Object (readonly)
Returns the value of attribute fixtures.
84 85 86 |
# File 'lib/active_shard/fixtures.rb', line 84 def fixtures @fixtures end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
81 82 83 |
# File 'lib/active_shard/fixtures.rb', line 81 def schema @schema end |
#shard ⇒ Object (readonly)
Returns the value of attribute shard.
82 83 84 |
# File 'lib/active_shard/fixtures.rb', line 82 def shard @shard end |
Instance Method Details
#connection ⇒ Object
98 99 100 |
# File 'lib/active_shard/fixtures.rb', line 98 def connection @connection ||= ::ActiveRecord::Base.connection_handler.connection_pool(@schema,@shard).connection end |
#reset! ⇒ Object
129 130 131 |
# File 'lib/active_shard/fixtures.rb', line 129 def reset! ::Fixtures.reset_cache connection end |
#setup_fixture_accessors(obj) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/active_shard/fixtures.rb', line 102 def setup_fixture_accessors obj fixture_names = self.fixture_names loaded_fixtures = ::Fixtures.cache_for_connection(connection) fixture_names.each do |table_name| table_name = table_name.to_s.tr('./', '_') obj.class.send(:define_method, table_name) do |*fixtures| force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload @fixture_cache ||= {} @fixture_cache[table_name] ||= {} instances = fixtures.map do |fixture| @fixture_cache[table_name].delete(fixture) if force_reload if loaded_fixtures[table_name][fixture.to_s] @fixture_cache[table_name][fixture] ||= loaded_fixtures[table_name][fixture.to_s].find else raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'" end end instances.size == 1 ? instances.first : instances end end end |