-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract Cacher class from Sparkline logic. It's now easy to replace t…
…he Filesystem cacher with something more interesting
- Loading branch information
1 parent
3b3f770
commit 7dc1cd4
Showing
4 changed files
with
82 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'rack-sparklines' | ||
require 'time' | ||
|
||
class Rack::Sparklines | ||
module Cachers | ||
# Reads sparkline data from CSV files. Only the first line of numbers are | ||
# read. Requests for "/sparks/stats.csv" will pass a data_path of "stats.csv" | ||
class Filesystem | ||
attr_accessor :png_path | ||
attr_accessor :directory | ||
|
||
def initialize(directory) | ||
@directory = directory | ||
@size, @updated_at = nil | ||
end | ||
|
||
# Setting the png_path returns a duplicate of this object that has any | ||
# custom instance variables (configuration settings, for example). | ||
def set(png_path) | ||
cacher = dup | ||
cacher.png_path = png_path | ||
cacher | ||
end | ||
|
||
def png_path=(s) | ||
@cache_file = File.join(@directory, s) | ||
@png_path = s | ||
end | ||
|
||
def size | ||
@size ||= File.size(@cache_file) | ||
end | ||
|
||
def exists? | ||
File.file?(@cache_file) | ||
end | ||
|
||
def updated_at | ||
@updated_at ||= File.mtime(@cache_file) | ||
end | ||
|
||
def save(data, options) | ||
File.open(@cache_file, 'wb') do |png| | ||
png << Spark.plot(data, options) | ||
end | ||
end | ||
|
||
def stream | ||
::File.open(@cache_file, "rb") do |file| | ||
while part = file.read(8192) | ||
yield part | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters