Class: JsonCli::Command::Unwind

Inherits:
Base
  • Object
show all
Defined in:
lib/json_cli/command/unwind.rb

Overview

Unwind JSON class

Instance Method Summary collapse

Constructor Details

#initialize(io, options) ⇒ Unwind

Returns a new instance of Unwind.



8
9
10
11
12
13
14
15
# File 'lib/json_cli/command/unwind.rb', line 8

def initialize(io, options)
  super(options)
  @io = io
  @unwind_key = options[:unwind_key]
  @flatten = options[:flatten]
  @key_label = options[:key_label] || 'key'
  @value_label = options[:value_label] || 'value'
end

Instance Method Details

#unwind_arrayObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/json_cli/command/unwind.rb', line 17

def unwind_array
  @io.each do |line|
    obj = MultiJson.load(line.chomp)
    if !obj.key?(@unwind_key) || !obj[@unwind_key].is_a?(Array)
      @output.puts MultiJson.dump(obj)
    else
      unwind_array_obj(obj)
    end
  end
end

#unwind_hashObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/json_cli/command/unwind.rb', line 28

def unwind_hash
  @io.each do |line|
    obj = MultiJson.load(line.chomp)
    if !obj.key?(@unwind_key) || !obj[@unwind_key].is_a?(Hash)
      @output.puts MultiJson.dump(obj)
    elsif @flatten
      unwind_hash_obj_flatten(obj)
    else
      unwind_hash_obj(obj)
    end
  end
end