Sha256: a4eb9a8fe91c4b57204a0a025a2d39a69b1115f23a5dd7f9297a76984615f174

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

require 'json'

module Norikra
  class Stats
    attr_accessor :targets, :queries

    def self.generate(engine)
      Norikra::Stats.new(
        targets: engine.targets.map{|t|
          {
            name: t.name,
            fields: engine.typedef_manager.dump_target(t.name),
            auto_field: t.auto_field
          }
        },
        queries: engine.queries.map(&:dump)
      )
    end

    def initialize(opts={})
      @targets = opts[:targets] || []
      @queries = opts[:queries] || []
    end

    def to_hash
      {targets: @targets, queries: @queries}
    end

    def to_json
      JSON.pretty_generate(self.to_hash)
    end

    def dump(path, secondary_path)
      tmp_path = path + '.tmp'
      File.open(tmp_path, 'w') do |file|
        file.write(self.to_json)
      end
      File.rename(tmp_path, path)

      if secondary_path
        require 'fileutils'
        secondary_actual_path = Time.now.strftime(secondary_path)
        FileUtils.copy(path, secondary_actual_path)
      end
    end

    def self.load(path)
      File.open(path, 'r') do |file|
        self.new(JSON.parse(file.read, symbolize_names: true))
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
norikra-1.2.2-java lib/norikra/stats.rb
norikra-1.2.1-java lib/norikra/stats.rb
norikra-1.2.0-java lib/norikra/stats.rb