Module: DS::Source::SourceCache

Included in:
BaseSource
Defined in:
lib/ds/source/source_cache.rb

Overview

This module provides methods for caching and opening source files. It is used by the DS::Mapper::BaseMapper class.

It makes available a #find_or_open_source method that can be used by the including class to open or retrieve a parse source file from the cache.

Including classes must implement the open_source method.

The file path is used as the cache key.

The initial cache size is the value of DS::Util::Cache::DEFAULT_MAX_SIZE.

Cache max size can be set and retrieved using the max_cache_size and max_cache_size= methods. #

Instance Method Summary collapse

Instance Method Details

#cacheDS::Util::Cache

Returns the cache object.

This method lazily initializes the cache object if it is not already initialized. The cache object is an instance of the DS::Util::Cache class.

Returns:



49
50
51
# File 'lib/ds/source/source_cache.rb', line 49

def cache
  @cache ||= DS::Util::Cache.new
end

#find_or_open_source(source_path) ⇒ Object

Finds or opens a source file at the given path.

Parameters:

  • source_path (String)

    the path to the source file

Returns:

  • (Object)

    the contents of the source file



27
28
29
30
31
32
# File 'lib/ds/source/source_cache.rb', line 27

def find_or_open_source source_path
  return cache.get_item source_path if cache.include? source_path
  source = open_source source_path
  cache.add source_path, source
  source
end

#max_cache_sizeInteger

Returns the maximum cache size.

Returns:

  • (Integer)

    the maximum number of items to store in the cache



64
65
66
# File 'lib/ds/source/source_cache.rb', line 64

def max_cache_size
  cache.max_size
end

#max_cache_size=(size) ⇒ void

This method returns an undefined value.

Sets the maximum cache size.

Parameters:

  • size (Integer)

    the maximum number of items to store in the cache



57
58
59
# File 'lib/ds/source/source_cache.rb', line 57

def max_cache_size= size
  cache.max_size = size
end

#open_source(source_path) ⇒ Object

Opens a source file at the given path.

Parameters:

  • source_path (String)

    the path to the source file

Returns:

  • (Object)

    the contents of the source file

Raises:

  • (NotImplementedError)

    unless implemented by including class



39
40
41
# File 'lib/ds/source/source_cache.rb', line 39

def open_source source_path
  raise NotImplementedError
end