Class: MetraSchedule::Cacher

Inherits:
Object
  • Object
show all
Defined in:
lib/metra/cacher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCacher

Returns a new instance of Cacher.



8
9
10
# File 'lib/metra/cacher.rb', line 8

def initialize
  @cache_dir = "#{ENV['HOME']}/.metra_schedule"
end

Instance Attribute Details

#cache_dirObject

Returns the value of attribute cache_dir.



6
7
8
# File 'lib/metra/cacher.rb', line 6

def cache_dir
  @cache_dir
end

Class Method Details

.load_from_cache(line) ⇒ Object



12
13
14
# File 'lib/metra/cacher.rb', line 12

def self.load_from_cache(line)
  self.new.retrieve_line(line)
end

.store_to_cache(line) ⇒ Object



16
17
18
# File 'lib/metra/cacher.rb', line 16

def self.store_to_cache(line)
  self.new.persist_line(line)
end

Instance Method Details

#check_for_metra_cache_dirObject



29
30
31
# File 'lib/metra/cacher.rb', line 29

def check_for_metra_cache_dir
  File.exists?(@cache_dir) and File.directory?(@cache_dir)
end

#create_cache_dir_if_not_existsObject



20
21
22
23
24
25
26
27
# File 'lib/metra/cacher.rb', line 20

def create_cache_dir_if_not_exists
  unless check_for_metra_cache_dir
    create_metra_cache_dir 
    true
  else
    false
  end
end

#create_metra_cache_dirObject



33
34
35
# File 'lib/metra/cacher.rb', line 33

def create_metra_cache_dir
  FileUtils.mkdir(@cache_dir)      
end

#line_exists?(line) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/metra/cacher.rb', line 37

def line_exists?(line)
  filename = line.line_key.to_s
  if File.exists?(File.join(@cache_dir, filename))
    true
  else
    false
  end
end

#persist_line(line) ⇒ Object



46
47
48
49
50
# File 'lib/metra/cacher.rb', line 46

def persist_line(line)
  create_cache_dir_if_not_exists
  return false unless line.engines
  true if File.open(File.join(@cache_dir, line.line_key.to_s), 'w') {|f| Marshal.dump(line.engines, f)}
end

#retrieve_line(line) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/metra/cacher.rb', line 52

def retrieve_line(line)
  if line_exists?(line)
    File.open(File.join(@cache_dir, line.line_key.to_s), 'r') {|f| Marshal.load(f)}
  else
    nil
  end
end