Class: Middleman::Presentation::Cache

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/middleman-presentation-core/cache.rb

Overview

Cache for objects

It can be used to run ‘block’ on ‘#map` only if new objects have been added.

Instance Method Summary collapse

Constructor Details

#initialize(store: []) ⇒ Cache

Returns a new instance of Cache.



18
19
20
21
# File 'lib/middleman-presentation-core/cache.rb', line 18

def initialize(store: [])
  @store = store
  @clean = true
end

Instance Method Details

#add(obj) ⇒ Object



23
24
25
26
27
28
# File 'lib/middleman-presentation-core/cache.rb', line 23

def add(obj)
  store << obj
  self.clean = false

  self
end

#each(&block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/middleman-presentation-core/cache.rb', line 36

def each(&block)
  return if clean

  self.clean = true

  store.each(&block)
end

#mark_dirtyObject



30
31
32
33
34
# File 'lib/middleman-presentation-core/cache.rb', line 30

def mark_dirty
  self.clean = false

  self
end

#to_aObject



44
45
46
# File 'lib/middleman-presentation-core/cache.rb', line 44

def to_a
  store.to_a
end