Class: StyleCache

Inherits:
Object
  • Object
show all
Defined in:
lib/hubdown/style_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStyleCache

Returns a new instance of StyleCache.



9
10
11
# File 'lib/hubdown/style_cache.rb', line 9

def initialize
  @latest_file = File.join(File.dirname(File.expand_path(__FILE__)), "cache/latest_styles.txt")
end

Instance Attribute Details

#latest_fileObject

Returns the value of attribute latest_file.



7
8
9
# File 'lib/hubdown/style_cache.rb', line 7

def latest_file
  @latest_file
end

#style_dataObject

Returns the value of attribute style_data.



5
6
7
# File 'lib/hubdown/style_cache.rb', line 5

def style_data
  @style_data
end

#tmpObject

Returns the value of attribute tmp.



6
7
8
# File 'lib/hubdown/style_cache.rb', line 6

def tmp
  @tmp
end

Instance Method Details

#create_tmp_fileObject



29
30
31
32
# File 'lib/hubdown/style_cache.rb', line 29

def create_tmp_file
  @tmp = get_cache_file "tmp.txt"
  FileUtils.touch tmp
end

#get_cache_file(name) ⇒ Object



59
60
61
# File 'lib/hubdown/style_cache.rb', line 59

def get_cache_file name
  File.join(File.dirname(File.expand_path(__FILE__)), "cache/#{name}")
end


14
15
16
# File 'lib/hubdown/style_cache.rb', line 14

def get_css_links
  load_latest_files
end

#get_style_content(filename) ⇒ Object



77
78
79
80
81
82
# File 'lib/hubdown/style_cache.rb', line 77

def get_style_content filename
  file = get_cache_file filename
  contents = ""
  File.open( file, 'r' ) {|f| contents = f.read() }
  contents
end

#load_latest_filesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hubdown/style_cache.rb', line 63

def load_latest_files
  cached_links = []
  File.readlines( @latest_file ).each do |line|
    contents = line.split(",")
    ss = StyleSheet.new
    ss.name = contents[0]
    ss.url = contents[1]
    ss.url = "file://" + (get_cache_file(''))
    ss.content = get_style_content ss.name
    cached_links << ss
  end
  cached_links
end

#remove_old_filesObject



50
51
52
53
54
55
56
57
# File 'lib/hubdown/style_cache.rb', line 50

def remove_old_files
  prev_sheets = load_latest_files
  prev_sheets.each do |sheet|
    FileUtils.rm(get_cache_file( sheet.name ))
  end
  File.open( @latest_file, 'w') {|f| f.write( style_data ) }
  FileUtils.rm tmp
end


18
19
20
21
22
23
24
25
26
27
# File 'lib/hubdown/style_cache.rb', line 18

def save_links links
  links.each do |link|
    link.download_content
  end
  create_tmp_file
  write_link_cache_file links
  write_link_contents links
  remove_old_files

end


34
35
36
37
38
39
40
# File 'lib/hubdown/style_cache.rb', line 34

def write_link_cache_file links
  @style_data = ""
  links.each do |link|
    style_data << "#{link.name},#{link.url}\n"
  end
  File.open( tmp, 'w') {|f| f.write( style_data ) }
end


42
43
44
45
46
47
48
# File 'lib/hubdown/style_cache.rb', line 42

def write_link_contents links
  links.each do |link|
    style_file = get_cache_file link.name
    FileUtils.touch style_file
    File.open(style_file, 'w') {|f| f.write( link.content ) }
  end
end