Class: Norikra::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/norikra/stats.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Stats

Returns a new instance of Stats.



20
21
22
23
# File 'lib/norikra/stats.rb', line 20

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

Instance Attribute Details

#queriesObject

Returns the value of attribute queries.



5
6
7
# File 'lib/norikra/stats.rb', line 5

def queries
  @queries
end

#targetsObject

Returns the value of attribute targets.



5
6
7
# File 'lib/norikra/stats.rb', line 5

def targets
  @targets
end

Class Method Details

.generate(engine) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/norikra/stats.rb', line 7

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

.load(path) ⇒ Object



47
48
49
50
51
# File 'lib/norikra/stats.rb', line 47

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

Instance Method Details

#dump(path, secondary_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/norikra/stats.rb', line 33

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

#to_hashObject



25
26
27
# File 'lib/norikra/stats.rb', line 25

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

#to_jsonObject



29
30
31
# File 'lib/norikra/stats.rb', line 29

def to_json
  JSON.pretty_generate(self.to_hash)
end