Class: ConvenientService::Support::Cache::Entities::Caches::Array

Inherits:
Base
  • Object
show all
Defined in:
lib/convenient_service/support/cache/entities/caches/array.rb,
lib/convenient_service/support/cache/entities/caches/array/entities/pair.rb

Direct Known Subclasses

ThreadSafeArray

Defined Under Namespace

Modules: Entities

Instance Attribute Summary

Attributes inherited from Base

#default, #key, #parent, #store

Instance Method Summary collapse

Methods inherited from Base

#==, #[], #[]=, #get, keygen, #keygen, #set

Methods included from AbstractMethod

abstract_method

Constructor Details

#initialize(store: [], **kwargs) ⇒ void



20
21
22
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 20

def initialize(store: [], **kwargs)
  super(store: ::Array.new(0, kwargs[:default]).concat(store), **kwargs)
end

Instance Method Details

#backendSymbol

Returns:

  • (Symbol)


27
28
29
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 27

def backend
  Constants::Backends::ARRAY
end

#clearConvenientService::Support::Cache::Entities::Caches::Array



100
101
102
103
104
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 100

def clear
  store.clear

  self
end

#default=(value) ⇒ Object

Returns Can be any type.

Parameters:

  • value (Object)

    Can be any type.

Returns:

  • (Object)

    Can be any type.



144
145
146
147
148
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 144

def default=(value)
  save_self_as_scope_in_parent!

  @default = value
end

#delete(key) ⇒ Object

Returns Can be any type.

Parameters:

  • key (Object)

    Can be any type.

Returns:

  • (Object)

    Can be any type.



87
88
89
90
91
92
93
94
95
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 87

def delete(key)
  index = index(key)

  value = store.delete_at(index).value if index

  delete_self_as_scope_in_parent! if store.empty?

  value
end

#empty?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 34

def empty?
  store.empty?
end

#exist?(key) ⇒ Boolean

Parameters:

  • key (Object)

    Can be any type.

Returns:

  • (Boolean)


42
43
44
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 42

def exist?(key)
  index(key) ? true : false
end

#fetch(key, &block) ⇒ Object

Returns Can be any type.

Parameters:

  • key (Object)

    Can be any type.

  • block (Proc, nil)

Returns:

  • (Object)

    Can be any type.



79
80
81
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 79

def fetch(key, &block)
  _fetch(key, default: default, &block)
end

#read(key) ⇒ Object

Returns Can be any type.

Parameters:

  • key (Object)

    Can be any type.

Returns:

  • (Object)

    Can be any type.



50
51
52
53
54
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 50

def read(key)
  index = index(key)

  index ? store[index].value : default
end

#scope(key, backed_by: backend, default: self.default) ⇒ ConvenientService::Support::Cache::Entities::Caches::Base

Creates a scoped cache. Parent cache is modified on the first write to the scoped cache.

Parameters:

  • key (Object)

    Can be any type.

  • backed_by (Symbol) (defaults to: backend)
  • default (Object) (defaults to: self.default)

    Can be any type.

Returns:



117
118
119
120
121
122
123
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 117

def scope(key, backed_by: backend, default: self.default)
  Utils.with_one_time_object do |undefined|
    value = _fetch(key, default: undefined)

    (value == undefined) ? Support::Cache.backed_by(backed_by).new(default: default, parent: self, key: key) : value
  end
end

#scope!(key, backed_by: backend, default: self.default) ⇒ ConvenientService::Support::Cache::Entities::Caches::Base

Creates a scoped cache. Parent cache is modified immediately.

Parameters:

  • key (Object)

    Can be any type.

  • backed_by (Symbol) (defaults to: backend)
  • default (Object) (defaults to: self.default)

    Can be any type.

Returns:



136
137
138
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 136

def scope!(key, backed_by: backend, default: self.default)
  _fetch(key) { Support::Cache.backed_by(backed_by).new(default: default, parent: self, key: key) }
end

#write(key, value) ⇒ Object

Returns Can be any type.

Parameters:

  • key (Object)

    Can be any type.

  • value (Object)

    Can be any type.

Returns:

  • (Object)

    Can be any type.



61
62
63
64
65
66
67
68
69
# File 'lib/convenient_service/support/cache/entities/caches/array.rb', line 61

def write(key, value)
  index = index(key) || store.size

  save_self_as_scope_in_parent!

  store[index] = pair(key, value)

  value
end