Class: Mnemonic::Sink::JSON

Inherits:
Object
  • Object
show all
Defined in:
lib/mnemonic/sink/json.rb

Instance Method Summary collapse

Constructor Details

#initialize(mnemonic, to = STDOUT, options = {}) ⇒ JSON

Returns a new instance of JSON.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mnemonic/sink/json.rb', line 6

def initialize(mnemonic, to = STDOUT, options = {})
  @mnemonic = mnemonic
  @io = if to.kind_of? String
          @need_close = true
          File.open(to, 'w')
        else
          to
        end
  @extra_enabled = options.delete(:extra)
  @row = {}
  @first = true
end

Instance Method Details

#closeObject



35
36
37
38
# File 'lib/mnemonic/sink/json.rb', line 35

def close
  @io << ']'.freeze
  @io.close if @need_close
end

#drop!(extra) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mnemonic/sink/json.rb', line 19

def drop!(extra)
  @mnemonic.metrics.each do |metric|
    @row[metric.name] = metric.value
  end
  @row['extra'.freeze] = extra if @extra_enabled
  json_row = ::JSON.dump(@row)
  if @first
    @io << '['.freeze
    @io << json_row
    @first = false
  else
    @io << ','.freeze
    @io << json_row
  end
end