Class: Sox::Stats::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/sox/stats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sound_file) ⇒ Cache

Returns a new instance of Cache.



33
34
35
# File 'lib/sox/stats.rb', line 33

def initialize(sound_file)
  @sound_file = sound_file
end

Instance Attribute Details

#sound_fileObject

Returns the value of attribute sound_file.



31
32
33
# File 'lib/sox/stats.rb', line 31

def sound_file
  @sound_file
end

Instance Method Details

#cache_fileObject



37
38
39
# File 'lib/sox/stats.rb', line 37

def cache_file
  "#{sound_file}.stats"
end

#dump(attributes) ⇒ Object



45
46
47
48
49
50
# File 'lib/sox/stats.rb', line 45

def dump(attributes)
  File.open(cache_file, "w") do |f|
    f.write YAML.dump(attributes)
  end
  attributes
end

#fetch(&block) ⇒ Object



52
53
54
# File 'lib/sox/stats.rb', line 52

def fetch(&block)
  load or dump(block.call)
end

#loadObject



41
42
43
# File 'lib/sox/stats.rb', line 41

def load
  YAML.load(IO.read(cache_file)) rescue nil
end