class Graffable::Importer

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/graffable/importer.rb, line 7
def initialize
  @db      = Graffable::Database.connect
  @groups  = {}
  @reports = {}
  yield self if block_given?
  self
end

Public Instance Methods

insert( group_name, report_name, value, params = {} ) click to toggle source
# File lib/graffable/importer.rb, line 19
def insert( group_name, report_name, value, params = {} )
  defaults = {
    day: nil, hour: nil, label: nil, month: nil, year: nil
  }
  opts     = defaults.merge(params)
  r        = _find_report group_name, report_name

  # TODO Silently skip existing rows?  And why isn't the database enforcing this?
  return if @db[:numbers][ report_id: r[:id], year: opts[:year], month: opts[:month], day: opts[:day], hour: opts[:hour], label: opts[:label] ]

  @db.transaction do
    rv = @db[:numbers].insert report_id: r[:id],
                              value:     value,
                              year:      opts[:year],
                              month:     opts[:month],
                              day:       opts[:day],
                              hour:      opts[:hour],
                              label:     opts[:label]
    raise "insert failed - #{rv.inspect}" unless rv > 0
  end
end
transaction(&block) click to toggle source
# File lib/graffable/importer.rb, line 15
def transaction(&block)
  @db.transaction block
end

Private Instance Methods

_find_report(group, report) click to toggle source
# File lib/graffable/importer.rb, line 44
def _find_report(group, report)
  unless @groups.key?(group)
    @groups[group] = @db[:groups][ name: group ]
  end
  g = @groups[group]
  raise "invalid group - #{group}" unless g

  unless @reports.key?(group)
    @reports[group] = {}
    @db[:reports].where( group_id: g[:id] ).each { |row| @reports[group][ row[:name] ] = row }
  end
  r = @reports[group][report]
  raise "invalid report - #{report}" unless r

  r
end