Class: FixtureKit::Registry

Inherits:
Object
  • Object
show all
Includes:
ConfigurationHelper
Defined in:
lib/fixture_kit/registry.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



7
8
9
10
11
12
# File 'lib/fixture_kit/registry.rb', line 7

def initialize
  @declarations = {}
  @fixtures = {}
  @resolving = []
  @cache_owners = {}
end

Instance Method Details

#add(name_or_definition, scope = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fixture_kit/registry.rb', line 14

def add(name_or_definition, scope = nil)
  fixture =
    case name_or_definition
    when String
      fetch_named_fixture(name_or_definition)
    when Definition
      raise ArgumentError, "scope is required for anonymous fixture declarations" unless scope
      fetch_anonymous_fixture(scope, name_or_definition)
    else
      raise FixtureKit::InvalidFixtureDeclaration, "unsupported fixture declaration type: #{name_or_definition.class}"
    end

  if scope
    raise FixtureKit::MultipleFixtures, "cannot load multiple fixtures in the same context" if @declarations.key?(scope)
    @declarations[scope] = fixture
  end

  fixture
end

#claim_cache_identifier(fixture) ⇒ Object

Fixtures sharing a cache identifier share a cache file: the second one to generate finds the file already written, skips generation, and mounts the other's records. That is only safe when both declarations are the same, so anything else has to fail here -- otherwise it surfaces as a confusing NoMethodError on Repository, or not at all when both expose the same names. Identifiers are derived from the declaration site, so this is a backstop against a digest collision or a regression in that derivation.



45
46
47
48
49
50
51
52
53
# File 'lib/fixture_kit/registry.rb', line 45

def claim_cache_identifier(fixture)
  owner = (@cache_owners[fixture.cache.identifier] ||= fixture)
  return if owner.equal?(fixture)
  return if owner.definition.fingerprint == fixture.definition.fingerprint

  raise FixtureKit::CacheIdentifierCollision,
    "fixtures declared at #{owner.definition.location} and #{fixture.definition.location} " \
    "both resolve to the cache identifier '#{fixture.cache.identifier}'"
end

#fixturesObject



34
35
36
# File 'lib/fixture_kit/registry.rb', line 34

def fixtures
  @fixtures.values
end