class Qntf::Json::Dataset

Attributes

data[RW]
fields[RW]
name[RW]
path[RW]

Public Class Methods

all() click to toggle source
# File lib/qntf/storage/json.rb, line 8
def self.all
  files = Dir.glob(source + '/*.json')
  files.map { |x| new(File.basename(x, '.json')) }
end
new(name, fields = []) click to toggle source
# File lib/qntf/storage/json.rb, line 19
def initialize(name, fields = [])
  @name = name
  @fields = fields
  @data = {}

  @path = File.expand_path("#{name}.json", self.class.source)

  if File.exist?(@path) and !File.zero?(@path)
    load
  else
    bootstrap
  end
end
source() click to toggle source
# File lib/qntf/storage/json.rb, line 13
def self.source
  ENV['QNTFDIR'] || "#{ENV['HOME']}/.qntf"
end

Public Instance Methods

add_datapoint(datapoint, date=nil) click to toggle source
# File lib/qntf/storage/json.rb, line 38
def add_datapoint(datapoint, date=nil)
  date ||= Time.now.utc.iso8601(6)
  data[date] = datapoint
  save
end
add_field(field) click to toggle source
# File lib/qntf/storage/json.rb, line 33
def add_field(field)
  @fields << field
  save
end
load() click to toggle source
# File lib/qntf/storage/json.rb, line 48
def load
  data = JSON.parse(File.read(@path))
  @fields = data['fields']
  @data =   data['data']
end
save() click to toggle source
# File lib/qntf/storage/json.rb, line 54
def save
  File.open(@path, 'w') do |file|
    file.puts to_hash.to_json
  end
end
to_hash() click to toggle source
# File lib/qntf/storage/json.rb, line 44
def to_hash
  {fields: @fields, data: @data}
end

Private Instance Methods

bootstrap() click to toggle source
# File lib/qntf/storage/json.rb, line 62
def bootstrap
  FileUtils.mkdir(self.class.source) unless File.directory?(self.class.source)
  FileUtils.touch(@path)
  save
end