Class: Jasmine::Headless::CacheableAction

Inherits:
Object
  • Object
show all
Defined in:
lib/jasmine/headless/cacheable_action.rb

Direct Known Subclasses

CoffeeScriptCache, SpecFileAnalyzer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ CacheableAction

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

#fileObject (readonly)

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_dirObject



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_typeObject

Raises:

  • (ArgumentError)


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

Returns:

  • (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

#actionObject

Raises:

  • (StandardError)


68
69
70
# File 'lib/jasmine/headless/cacheable_action.rb', line 68

def action
  raise StandardError.new("Override action")
end

#cache_fileObject



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

Returns:

  • (Boolean)


64
65
66
# File 'lib/jasmine/headless/cacheable_action.rb', line 64

def cached?
  File.exist?(cache_file)
end

#fresh?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/jasmine/headless/cacheable_action.rb', line 60

def fresh?
  cached? && (File.mtime(file) < File.mtime(cache_file))
end

#handleObject



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