Class: TypedCache::CacheBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_cache/cache_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(cache_definition = CacheDefinition.new, backend_registry = Backends, decorator_registry = Decorators) ⇒ CacheBuilder

Returns a new instance of CacheBuilder.



82
83
84
85
86
87
# File 'lib/typed_cache/cache_builder.rb', line 82

def initialize(cache_definition = CacheDefinition.new, backend_registry = Backends, decorator_registry = Decorators)
  @backend_registry = backend_registry
  @decorator_registry = decorator_registry

  @cache_definition = cache_definition
end

Instance Method Details

#build(namespace = Namespace.root) ⇒ Object

Builds the cache using the given namespace, defaulting to the root namespace



91
92
93
# File 'lib/typed_cache/cache_builder.rb', line 91

def build(namespace = Namespace.root)
  validate_and_build(namespace)
end

#build!(namespace = Namespace.root) ⇒ Object



96
97
98
# File 'lib/typed_cache/cache_builder.rb', line 96

def build!(namespace = Namespace.root)
  build(namespace).right_or_raise!
end

#build_backendObject

Constructs only a typed backend from the registry without a Store wrapper



102
103
104
# File 'lib/typed_cache/cache_builder.rb', line 102

def build_backend
  create_backend
end

#build_backend!Object

Constructs only a typed backend from the registry without a Store wrapper, raises an error if the backend cannot be constructed



110
111
112
# File 'lib/typed_cache/cache_builder.rb', line 110

def build_backend!
  create_backend.right_or_raise!
end

#with_backend(name, *args, **options) ⇒ Object

Familiar Ruby fluent interface - always succeeds Invalid configurations are caught during build()



117
118
119
120
121
122
123
# File 'lib/typed_cache/cache_builder.rb', line 117

def with_backend(name, *args, **options)
  self.class.new(
    @cache_definition.with_backend(name, *args, **options),
    @backend_registry,
    @decorator_registry,
  )
end

#with_decorator(name, **options) ⇒ Object

Adds an arbitrary decorator by registry key



127
128
129
130
131
132
133
# File 'lib/typed_cache/cache_builder.rb', line 127

def with_decorator(name, **options)
  self.class.new(
    @cache_definition.with_decorator(name, **options),
    @backend_registry,
    @decorator_registry,
  )
end

#with_instrumentation(source = :default) ⇒ Object

Adds instrumentation using the specified strategy.



137
138
139
140
141
142
143
# File 'lib/typed_cache/cache_builder.rb', line 137

def with_instrumentation(source = :default)
  self.class.new(
    @cache_definition.with_instrumentation(source),
    @backend_registry,
    @decorator_registry,
  )
end