Class: Jasmine::Headless::CacheableAction
- Inherits:
-
Object
- Object
- Jasmine::Headless::CacheableAction
show all
- Defined in:
- lib/jasmine/headless/cacheable_action.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of CacheableAction.
37
38
39
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 37
def initialize(file)
@file = file
end
|
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
35
36
37
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 35
def file
@file
end
|
Class Method Details
.cache_dir ⇒ Object
26
27
28
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 26
def cache_dir
@cache_dir ||= '.jhw-cache'
end
|
.cache_dir=(dir) ⇒ Object
22
23
24
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 22
def cache_dir=(dir)
@cache_dir = dir
end
|
.cache_type ⇒ Object
13
14
15
16
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 13
def cache_type
raise ArgumentError.new("No cache type defined for #{self.name}") if @cache_type == nil
@cache_type
end
|
.cache_type=(type) ⇒ Object
18
19
20
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 18
def cache_type=(type)
@cache_type = type
end
|
.enabled=(bool) ⇒ Object
4
5
6
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 4
def enabled=(bool)
@enabled = bool
end
|
.enabled? ⇒ Boolean
8
9
10
11
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 8
def enabled?
@enabled = true if @enabled == nil
@enabled
end
|
.for(file) ⇒ Object
30
31
32
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 30
def for(file)
new(file).handle
end
|
Instance Method Details
#action ⇒ Object
68
69
70
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 68
def action
raise StandardError.new("Override action")
end
|
#cache_file ⇒ Object
56
57
58
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 56
def cache_file
@cache_file ||= File.expand_path(File.join(self.class.cache_dir, self.class.cache_type, file)) + '.js'
end
|
#cached? ⇒ Boolean
64
65
66
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 64
def cached?
File.exist?(cache_file)
end
|
#fresh? ⇒ Boolean
60
61
62
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 60
def fresh?
cached? && (File.mtime(file) < File.mtime(cache_file))
end
|
#handle ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 41
def handle
if CacheableAction.enabled?
if fresh?
unserialize(File.read(cache_file))
else
result = action
FileUtils.mkdir_p File.split(cache_file).first
File.open(cache_file, 'wb') { |fh| fh.print serialize(result) }
result
end
else
action
end
end
|
#serialize(data) ⇒ Object
72
73
74
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 72
def serialize(data)
data
end
|
#unserialize(data) ⇒ Object
76
77
78
|
# File 'lib/jasmine/headless/cacheable_action.rb', line 76
def unserialize(data)
data
end
|