Class: Bindings::CacheBinding

Inherits:
Object
  • Object
show all
Includes:
Binding
Defined in:
lib/binding.rb

Overview

An implementation of Bindings::Binding that caches values once they’ve been retrieved.

Instance Method Summary collapse

Methods included from Binding

#get, #provider, #type

Constructor Details

#initialize(delegate) ⇒ CacheBinding

Creates a new instance.

Parameters:



88
89
90
91
# File 'lib/binding.rb', line 88

def initialize(delegate)
  @delegate = delegate
  @cache = {}
end

Instance Method Details

#get_as_bytes(key) ⇒ String?

Returns the contents of a binding entry in its raw bytes form.

Parameters:

  • key (String)

    the key of the entry to retrieve

Returns:

  • (String)

    the contents of a binding entry if it exists

  • (nil)


98
99
100
101
102
103
104
105
# File 'lib/binding.rb', line 98

def get_as_bytes(key)
  return @cache[key] if @cache.key?(key)

  v = @delegate.get_as_bytes(key)
  @cache[key] = v unless v.nil?

  v
end

#nameString

Returns the name of the binding

Returns:

  • (String)

    the name of the binding



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

def name
  @delegate.name
end