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.



60
61
62
63
64
65
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
# File 'lib/rbbt/tsv/manipulate.rb', line 60

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 (Fixnum === @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 Fixnum === @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 Fixnum === @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
    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.



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

def new_field_names
  @new_field_names
end

#new_fieldsObject

Returns the value of attribute new_fields.



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

def new_fields
  @new_fields
end

#new_key_fieldObject

Returns the value of attribute new_key_field.



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

def new_key_field
  @new_key_field
end

#new_key_field_nameObject

Returns the value of attribute new_key_field_name.



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

def new_key_field_name
  @new_key_field_name
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#uniqObject

Returns the value of attribute uniq.



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

def uniq
  @uniq
end

Instance Method Details

#process_null(key, values) ⇒ Object



10
11
12
# File 'lib/rbbt/tsv/manipulate.rb', line 10

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

#process_reorder_double(key, values) ⇒ Object



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

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



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

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



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

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

#process_reorder_list(key, values) ⇒ Object



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

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

#process_subset_all_but_double(key, values) ⇒ Object



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

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



18
19
20
21
22
# File 'lib/rbbt/tsv/manipulate.rb', line 18

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



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

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

#process_subset_list(key, values) ⇒ Object



14
15
16
# File 'lib/rbbt/tsv/manipulate.rb', line 14

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