Sha256: e9d25bc823896b30f448e2b3763a3fc9bdaa9c3ce1d1566c2fde406de63fc125

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 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', external_encoding: 'utf-8') do |file|
        self.new(JSON.parse(file.read, symbolize_names: true))
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
norikra-1.5.1-java lib/norikra/stats.rb
norikra-1.5.0-java lib/norikra/stats.rb
norikra-1.4.0-java lib/norikra/stats.rb
norikra-1.3.1-java lib/norikra/stats.rb
norikra-1.3.0-java lib/norikra/stats.rb
norikra-1.3.0.beta.1-java lib/norikra/stats.rb