Class: FixtureKit::Cache

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

Constant Summary collapse

ANONYMOUS_DIRECTORY =
"_anonymous"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fixture) ⇒ Cache

Returns a new instance of Cache.



11
12
13
# File 'lib/fixture_kit/cache.rb', line 11

def initialize(fixture)
  @fixture = fixture
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/fixture_kit/cache.rb', line 9

def content
  @content
end

#fixtureObject (readonly)

Returns the value of attribute fixture.



9
10
11
# File 'lib/fixture_kit/cache.rb', line 9

def fixture
  @fixture
end

Instance Method Details

#clear_memoryObject



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

def clear_memory
  @content = nil
end

#exists?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/fixture_kit/cache.rb', line 30

def exists?
  content || file_cache.exists?
end

#identifierObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/fixture_kit/cache.rb', line 19

def identifier
  @identifier ||= begin
    raw_identifier = fixture.identifier
    if raw_identifier.is_a?(String)
      raw_identifier
    else
      File.join(ANONYMOUS_DIRECTORY, FixtureKit.runner.adapter.identifier_for(raw_identifier))
    end
  end
end

#loadObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fixture_kit/cache.rb', line 38

def load
  unless exists?
    raise FixtureKit::CacheMissingError, "Cache does not exist for fixture '#{fixture.identifier}'"
  end

  @content ||= file_cache.read

  FixtureKit.runner.coders.each do |coder|
    coder.mount(content.data_for(coder.class))
  end

  Repository.new(content.exposed)
end

#pathObject



15
16
17
# File 'lib/fixture_kit/cache.rb', line 15

def path
  file_cache.path
end

#saveObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/fixture_kit/cache.rb', line 52

def save
  FixtureKit.runner.adapter.execute do |context|
    @content = MemoryCache.new(
      data: evaluate(FixtureKit.runner.coders, context),
      exposed: file_cache.serialize_exposed(fixture.definition.exposed)
    )
  end

  file_cache.write(content)
end