Class: TSV::Transformer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, dumper = nil, unnamed: nil, namespace: nil) ⇒ Transformer

Returns a new instance of Transformer.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/scout/tsv/transformer.rb', line 5

def initialize(parser, dumper = nil, unnamed: nil, namespace: nil)
  if TSV::Parser === parser
    @parser = parser
  elsif TSV === parser
    @parser = parser
    @unnamed = parser.unnamed
  else
    @parser = TSV::Parser.new parser
  end
  @unnamed = unnamed unless unnamed.nil?
  if dumper.nil?
    @dumper = TSV::Dumper.new(@parser)
    @dumper.sep = "\t"
  else
    @dumper = dumper
  end
end

Instance Attribute Details

#dumperObject

Returns the value of attribute dumper.



3
4
5
# File 'lib/scout/tsv/transformer.rb', line 3

def dumper
  @dumper
end

#namespaceObject

Returns the value of attribute namespace.



3
4
5
# File 'lib/scout/tsv/transformer.rb', line 3

def namespace
  @namespace
end

#parserObject

Returns the value of attribute parser.



3
4
5
# File 'lib/scout/tsv/transformer.rb', line 3

def parser
  @parser
end

#unnamedObject

Returns the value of attribute unnamed.



3
4
5
# File 'lib/scout/tsv/transformer.rb', line 3

def unnamed
  @unnamed
end

Instance Method Details

#[]=(key, value) ⇒ Object



102
103
104
105
# File 'lib/scout/tsv/transformer.rb', line 102

def []=(key, value)
  @dumper.init if @dumper.respond_to?(:init) && ! @dumper.initialized
  @dumper.add key, value
end

#all_fieldsObject



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

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

#each(*args, **kwargs, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/scout/tsv/transformer.rb', line 80

def each(*args, **kwargs, &block)
  kwargs[:into] = @dumper
  kwargs[:bar] = "Transform #{Log.fingerprint @parser} into #{Log.fingerprint @target}" if TrueClass === kwargs[:bar]
  @dumper.namespace ||= @namespace
  @dumper.init if @dumper.respond_to?(:init) && ! @dumper.initialized
  Open.traverse(@parser, *args, **kwargs) do |k,v|
    NamedArray.setup(v, @parser.fields, k) unless @unnamed
    block.call k, v
    [k, v]
  end
end

#fieldsObject



51
52
53
# File 'lib/scout/tsv/transformer.rb', line 51

def fields
  @dumper.fields
end

#fields=(fields) ⇒ Object



27
28
29
# File 'lib/scout/tsv/transformer.rb', line 27

def fields=(fields)
  @dumper.fields = fields
end

#identify_field(name) ⇒ Object



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

def identify_field(name)
  TSV.identify_field key_field, fields, name
end

#include?(*args) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/scout/tsv/transformer.rb', line 43

def include?(*args)
  false
end

#key_fieldObject



47
48
49
# File 'lib/scout/tsv/transformer.rb', line 47

def key_field
  @dumper.key_field
end

#key_field=(key_field) ⇒ Object



23
24
25
# File 'lib/scout/tsv/transformer.rb', line 23

def key_field=(key_field)
  @dumper.key_field = key_field
end

#optionsObject



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

def options
  @dumper.options
end

#sep=(sep) ⇒ Object



39
40
41
# File 'lib/scout/tsv/transformer.rb', line 39

def sep=(sep)
  @dumper.sep = sep
end

#streamObject



107
108
109
# File 'lib/scout/tsv/transformer.rb', line 107

def stream
  @dumper.stream
end

#traverse(*args, **kwargs, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/scout/tsv/transformer.rb', line 68

def traverse(*args, **kwargs, &block)
  kwargs[:into] = @dumper
  kwargs[:bar] = "Transform #{Log.fingerprint @parser} into #{Log.fingerprint @target}" if TrueClass === kwargs[:bar]
  @dumper.namespace ||= @namespace
  @dumper.init if @dumper.respond_to?(:init) && ! @dumper.initialized
  Log.debug "Transform #{Log.fingerprint @parser} into #{Log.fingerprint @dumper}"
  Open.traverse(@parser, *args, **kwargs) do |k,v|
    NamedArray.setup(v, @parser.fields, k) unless @unnamed || @parser.fields.nil?
    block.call k, v
  end
end

#tsv(*args) ⇒ Object



111
112
113
# File 'lib/scout/tsv/transformer.rb', line 111

def tsv(*args)
  TSV === @dumper ? @dumper : TSV.open(@dumper, *args)
end

#typeObject



35
36
37
# File 'lib/scout/tsv/transformer.rb', line 35

def type
  @dumper.type
end

#type=(type) ⇒ Object



31
32
33
# File 'lib/scout/tsv/transformer.rb', line 31

def type=(type)
  @dumper.type = type
end

#with_unnamedObject



92
93
94
95
96
97
98
99
100
# File 'lib/scout/tsv/transformer.rb', line 92

def with_unnamed
  begin
    old_unnamed = @unnamed
    @unnamed = true
    yield
  ensure
    @unnamed = old_unnamed
  end
end