class Librato::Store
Public Instance Methods
clear()
click to toggle source
# File lib/librato/store.rb, line 8 def clear puts "Clearing existing chart data in #{dir}." FileUtils.rm_r(dir) if File.directory?(dir) end
read()
click to toggle source
# File lib/librato/store.rb, line 22 def read puts "Reading chart data from #{dir}." Dir["#{dir}/*.json"].sort.map do |path| tmpl = File.read(path) json = Template.new(tmpl, vars).render JSON.parse(json) end end
write(chart)
click to toggle source
# File lib/librato/store.rb, line 13 def write(chart) FileUtils.mkdir_p(dir) json = JSON.pretty_generate(chart.deep_except('id')) tmpl = Template.new(json, vars).generate path = path(chart['name']) puts "Storing chart data to #{path}." File.open(path, 'w+') { |f| f.write(tmpl) } end
Private Instance Methods
dir()
click to toggle source
# File lib/librato/store.rb, line 47 def dir [options[:dir] || './var/librato', dir_name(space.name)].join('/') end
dir_name(string)
click to toggle source
# File lib/librato/store.rb, line 51 def dir_name(string) string.gsub(/[\W]/, ' ').strip.gsub(' ', ' ').gsub(' ', '_').downcase end
order()
click to toggle source
# File lib/librato/store.rb, line 39 def order space.config['order'] || [] end
path(name)
click to toggle source
# File lib/librato/store.rb, line 33 def path(name) name = name.gsub(/[\W]+/, ' ').downcase.strip.gsub(' ', '_') num = order.index { |n| name.include?(n) } || 0 path = "#{dir}/#{num.to_s.rjust(2, '0')}_chart_#{name}.json" end
vars()
click to toggle source
# File lib/librato/store.rb, line 43 def vars options[:vars] || {} end