Class: Mnemonic::Sink::JSON
- Inherits:
-
Object
- Object
- Mnemonic::Sink::JSON
- Defined in:
- lib/mnemonic/sink/json.rb
Instance Method Summary collapse
- #close ⇒ Object
- #drop!(extra) ⇒ Object
-
#initialize(mnemonic, to = STDOUT, options = {}) ⇒ JSON
constructor
A new instance of JSON.
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, = {}) @mnemonic = mnemonic @io = if to.kind_of? String @need_close = true File.open(to, 'w') else to end @extra_enabled = .delete(:extra) @row = {} @first = true end |
Instance Method Details
#close ⇒ Object
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 |