Class: TSV::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/scout/tsv/dumper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Dumper

Returns a new instance of Dumper.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/scout/tsv/dumper.rb', line 30

def initialize(options = {})
  options = options.options.merge(sep: nil) if TSV::Parser === options || TSV === options
  @sep, @type = IndiferentHash.process_options options, 
    :sep, :type, 
    :sep => "\t", :type => :double
  @options = options
  @options[:type] = @type
  @sout, @sin = Open.pipe
  Log.low{"Dumper pipe #{[Log.fingerprint(@sin), Log.fingerprint(@sout)] * " -> "}"}
  @initialized = false
  @filename = options[:filename]
  @mutex = Mutex.new
  ConcurrentStream.setup(@sin, pair: @sout)
  ConcurrentStream.setup(@sout, pair: @sin)
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



29
30
31
# File 'lib/scout/tsv/dumper.rb', line 29

def filename
  @filename
end

#initializedObject

Returns the value of attribute initialized.



29
30
31
# File 'lib/scout/tsv/dumper.rb', line 29

def initialized
  @initialized
end

#namespaceObject

Returns the value of attribute namespace.



29
30
31
# File 'lib/scout/tsv/dumper.rb', line 29

def namespace
  @namespace
end

#optionsObject

Returns the value of attribute options.



29
30
31
# File 'lib/scout/tsv/dumper.rb', line 29

def options
  @options
end

#sepObject

Returns the value of attribute sep.



29
30
31
# File 'lib/scout/tsv/dumper.rb', line 29

def sep
  @sep
end

#typeObject

Returns the value of attribute type.



29
30
31
# File 'lib/scout/tsv/dumper.rb', line 29

def type
  @type
end

Class Method Details

.header(options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/scout/tsv/dumper.rb', line 3

def self.header(options={})
  key_field, fields, sep, header_hash, preamble, unnamed = IndiferentHash.process_options options, 
    :key_field, :fields, :sep, :header_hash, :preamble, :unnamed,
    :sep => "\t", :header_hash => "#", :preamble => true

  if fields.nil?
    fields_str = nil
  elsif fields.empty?
    fields_str = "#{header_hash}#{key_field || "Id"}"
  else
    fields_str = "#{header_hash}#{key_field || "Id"}#{sep}#{fields*sep}"
  end

  if String === preamble
    preamble_str = preamble
  elsif preamble && options.values.compact.any?
    preamble_str = "#: " << IndiferentHash.hash2string(options.merge(serializer: nil))
  else
    preamble_str = nil
  end

  preamble_str = preamble_str.strip if preamble_str
  [preamble_str, fields_str].compact * "\n"
end

Instance Method Details

#abort(exception = nil) ⇒ Object



126
127
128
# File 'lib/scout/tsv/dumper.rb', line 126

def abort(exception=nil)
  @sin.abort(exception) if @sin.respond_to?(:abort)
end

#add(key, value) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/scout/tsv/dumper.rb', line 86

def add(key, value)
  @mutex.synchronize do

    key = key.to_s unless String === key
    if value.nil? || (Array === value && value.empty?)
      @sin << key << "\n"
    else
      case @type
      when :single
        @sin << key + @sep + value.to_s << "\n"
      when :list, :flat
        @sin << key + @sep + value * @sep << "\n"
      when :double
        @sin << key + @sep + value.collect{|v| Array === v ? v * "|" : v } * @sep << "\n"
      else
        if Array === value 
          if Array === value.first
            @sin << key + @sep + value.collect{|v| Array === v ? v * "|" : v } * @sep << "\n"
          else
            @sin << key + @sep + value * @sep << "\n"
          end
        else
          @sin << key + @sep + value.to_s << "\n"
        end
      end
    end
  end
end

#all_fieldsObject



72
73
74
75
# File 'lib/scout/tsv/dumper.rb', line 72

def all_fields
  return nil if fields.nil?
  [key_field] + fields
end

#closeObject



115
116
117
118
119
120
# File 'lib/scout/tsv/dumper.rb', line 115

def close
  if @sin != @sout
    @sin.close if @sin.respond_to?(:close) && ! @sin.closed?
    @sin.join if @sin.respond_to?(:join) && ! @sin.joined?
  end
end

#digest_strObject



138
139
140
# File 'lib/scout/tsv/dumper.rb', line 138

def digest_str
  fingerprint
end

#fieldsObject



56
57
58
# File 'lib/scout/tsv/dumper.rb', line 56

def fields
  @options[:fields]
end

#fields=(fields) ⇒ Object



64
65
66
# File 'lib/scout/tsv/dumper.rb', line 64

def fields=(fields)
  @options[:fields] = fields
end

#fingerprintObject



134
135
136
# File 'lib/scout/tsv/dumper.rb', line 134

def fingerprint
  "Dumper:{"<< Log.fingerprint(self.all_fields|| []) << "}"
end

#init(preamble: true) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/scout/tsv/dumper.rb', line 78

def init(preamble: true)
  header = Dumper.header(@options.merge(type: @type, sep: @sep, preamble: preamble))
  @mutex.synchronize do
    @initialized = true
    @sin << header << "\n" if header and ! header.empty?
  end
end

#inspectObject



142
143
144
# File 'lib/scout/tsv/dumper.rb', line 142

def inspect
  fingerprint
end

#key_fieldObject



52
53
54
# File 'lib/scout/tsv/dumper.rb', line 52

def key_field
  @options[:key_field]
end

#key_field=(key_field) ⇒ Object



60
61
62
# File 'lib/scout/tsv/dumper.rb', line 60

def key_field=(key_field)
  @options[:key_field] = key_field
end

#set_stream(stream) ⇒ Object



46
47
48
49
50
# File 'lib/scout/tsv/dumper.rb', line 46

def set_stream(stream)
  @sin.close
  @sout.close
  @sout = @sin = stream
end

#streamObject



122
123
124
# File 'lib/scout/tsv/dumper.rb', line 122

def stream
  @sout
end

#tsv(*args) ⇒ Object



130
131
132
# File 'lib/scout/tsv/dumper.rb', line 130

def tsv(*args)
  TSV.open(stream, *args)
end