Class: TSV::Traverser

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/tsv/manipulate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_field, fields, new_key_field, new_fields, type, uniq) ⇒ Traverser

Returns a new instance of Traverser.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/rbbt/tsv/manipulate.rb', line 66

def initialize(key_field, fields, new_key_field, new_fields, type, uniq)
  @new_key_field = TSV.identify_field(key_field, fields, new_key_field)

  raise "Key field #{ new_key_field } not found" if @new_key_field.nil?
  @new_fields = case new_fields
                when nil
                  case 
                  when @new_key_field == :key
                    :all
                  when fields.nil?
                    - @new_key_field
                  else 
                    new = (0..fields.length - 1).to_a
                    new.delete_at(@new_key_field)
                    new.unshift :key
                    new
                  end
                when Array
                  new_fields.collect do |field|
                    TSV.identify_field(key_field, fields, field)
                  end
                when String, Symbol
                  [TSV.identify_field(key_field, fields, new_fields)]
                else
                  raise "Unknown format for new_fields (should be nil, Array or String): #{new_fields.inspect}"
                end

  @new_key_field_name = case 
                        when @new_key_field == :key
                          key_field
                        else
                          fields[@new_key_field] if Array === fields
                        end

  if Array === fields
    @new_field_names = case
                       when fields.nil?
                         nil
                       when Array === @new_fields
                         @new_field_names = @new_fields.collect do |field|
                           case 
                           when field == :key
                             key_field
                           else
                             fields[field]
                           end
                         end
                       when @new_fields == :all
                         fields
                       when (Numeric === @new_fields and @new_fields <= 0)
                         new = fields.dup
                         new.delete_at(- @new_fields)
                         new.unshift key_field
                         new
                       end
  end

  case
  when (@new_key_field == :key and (@new_fields == :all or fields.nil? or @new_fields == (0..fields.length - 1).to_a))
    self.instance_eval do alias process process_null end
  when @new_key_field == :key 
    if type == :double
      if Numeric === @new_fields and @new_fields <= 0
        self.instance_eval do alias process process_subset_all_but_double end
      else
        self.instance_eval do alias process process_subset_double end
      end
    else
      if Numeric === @new_fields and @new_fields <= 0
        self.instance_eval do alias process process_subset_all_but_list end
      else
        self.instance_eval do alias process process_subset_list end
      end
    end
  else
    case type 
    when :double
      if uniq
        self.instance_eval do alias process process_reorder_double_uniq end
      else
        self.instance_eval do alias process process_reorder_double end
      end
    when :flat
      self.instance_eval do alias process process_reorder_flat end
    when :single
      self.instance_eval do alias process process_reorder_single end
    else
      self.instance_eval do alias process process_reorder_list end
    end
  end
end

Instance Attribute Details

#new_field_namesObject

Returns the value of attribute new_field_names.



6
7
8
# File 'lib/rbbt/tsv/manipulate.rb', line 6

def new_field_names
  @new_field_names
end

#new_fieldsObject

Returns the value of attribute new_fields.



6
7
8
# File 'lib/rbbt/tsv/manipulate.rb', line 6

def new_fields
  @new_fields
end

#new_key_fieldObject

Returns the value of attribute new_key_field.



6
7
8
# File 'lib/rbbt/tsv/manipulate.rb', line 6

def new_key_field
  @new_key_field
end

#new_key_field_nameObject

Returns the value of attribute new_key_field_name.



6
7
8
# File 'lib/rbbt/tsv/manipulate.rb', line 6

def new_key_field_name
  @new_key_field_name
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/rbbt/tsv/manipulate.rb', line 6

def type
  @type
end

#uniqObject

Returns the value of attribute uniq.



6
7
8
# File 'lib/rbbt/tsv/manipulate.rb', line 6

def uniq
  @uniq
end

Instance Method Details

#process_null(key, values) ⇒ Object



8
9
10
# File 'lib/rbbt/tsv/manipulate.rb', line 8

def process_null(key, values)
  [[key], values]
end

#process_reorder_double(key, values) ⇒ Object



52
53
54
55
56
57
# File 'lib/rbbt/tsv/manipulate.rb', line 52

def process_reorder_double(key, values)
  [ values[@new_key_field], 
    @new_fields.collect{|field| field == :key ?  
      [key] : values[field] }
  ]
end

#process_reorder_double_uniq(key, values) ⇒ Object



35
36
37
38
39
40
# File 'lib/rbbt/tsv/manipulate.rb', line 35

def process_reorder_double_uniq(key, values)
  [ values[@new_key_field].uniq, 
    @new_fields.collect{|field| field == :key ?  
      [key] : values[field] }
  ]
end

#process_reorder_flat(key, values) ⇒ Object



59
60
61
62
63
64
# File 'lib/rbbt/tsv/manipulate.rb', line 59

def process_reorder_flat(key, values)
  [ values,
    @new_fields.collect{|field| field == :key ?  
      [key] : values[field] }.flatten
  ]
end

#process_reorder_list(key, values) ⇒ Object



30
31
32
33
# File 'lib/rbbt/tsv/manipulate.rb', line 30

def process_reorder_list(key, values)
  [ [values[@new_key_field]], 
    @new_fields.collect{|field| field == :key ?  key : values[field] }]
end

#process_reorder_single(key, values) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rbbt/tsv/manipulate.rb', line 22

def process_reorder_single(key, values)
  new_key = @new_key_field == :key ? key : values
  new_value = @new_fields.collect{|field| field == :key ?  key : values }.first
  return [new_key, new_value]
  [ [values[@new_key_field]], 
    @new_fields.collect{|field| field == :key ?  key : values[field] }]
end

#process_subset_all_but_double(key, values) ⇒ Object



46
47
48
49
50
# File 'lib/rbbt/tsv/manipulate.rb', line 46

def process_subset_all_but_double(key, values)
  new = values.dup
  new.delete_at(0 - @new_fields)
  [[key], new]
end

#process_subset_all_but_list(key, values) ⇒ Object



16
17
18
19
20
# File 'lib/rbbt/tsv/manipulate.rb', line 16

def process_subset_all_but_list(key, values)
  new = values.dup
  new.delete_at(0 - @new_fields)
  [key, new]
end

#process_subset_double(key, values) ⇒ Object



42
43
44
# File 'lib/rbbt/tsv/manipulate.rb', line 42

def process_subset_double(key, values)
  [[key], @new_fields.collect{|field| field == :key ?  [key] : values[field] }]
end

#process_subset_list(key, values) ⇒ Object



12
13
14
# File 'lib/rbbt/tsv/manipulate.rb', line 12

def process_subset_list(key, values)
  [key, @new_fields.collect{|field| field == :key ?  key : values[field] }]
end