Class: TSV::Traverser
- Inherits:
-
Object
- Object
- TSV::Traverser
- Defined in:
- lib/rbbt/tsv/manipulate.rb
Instance Attribute Summary collapse
-
#new_field_names ⇒ Object
Returns the value of attribute new_field_names.
-
#new_fields ⇒ Object
Returns the value of attribute new_fields.
-
#new_key_field ⇒ Object
Returns the value of attribute new_key_field.
-
#new_key_field_name ⇒ Object
Returns the value of attribute new_key_field_name.
-
#type ⇒ Object
Returns the value of attribute type.
-
#uniq ⇒ Object
Returns the value of attribute uniq.
Instance Method Summary collapse
-
#initialize(key_field, fields, new_key_field, new_fields, type, uniq) ⇒ Traverser
constructor
A new instance of Traverser.
- #process_null(key, values) ⇒ Object
- #process_reorder_double(key, values) ⇒ Object
- #process_reorder_double_uniq(key, values) ⇒ Object
- #process_reorder_flat(key, values) ⇒ Object
- #process_reorder_list(key, values) ⇒ Object
- #process_reorder_single(key, values) ⇒ Object
- #process_subset_all_but_double(key, values) ⇒ Object
- #process_subset_all_but_list(key, values) ⇒ Object
- #process_subset_double(key, values) ⇒ Object
- #process_subset_list(key, values) ⇒ Object
Constructor Details
#initialize(key_field, fields, new_key_field, new_fields, type, uniq) ⇒ Traverser
Returns a new instance of Traverser.
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 157 158 |
# File 'lib/rbbt/tsv/manipulate.rb', line 68 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_names ⇒ Object
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_fields ⇒ Object
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_field ⇒ Object
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_name ⇒ Object
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 |
#type ⇒ Object
Returns the value of attribute type.
8 9 10 |
# File 'lib/rbbt/tsv/manipulate.rb', line 8 def type @type end |
#uniq ⇒ Object
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
54 55 56 57 58 59 |
# File 'lib/rbbt/tsv/manipulate.rb', line 54 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
37 38 39 40 41 42 |
# File 'lib/rbbt/tsv/manipulate.rb', line 37 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
61 62 63 64 65 66 |
# File 'lib/rbbt/tsv/manipulate.rb', line 61 def process_reorder_flat(key, values) [ values, @new_fields.collect{|field| field == :key ? [key] : values[field] }.flatten ] end |
#process_reorder_list(key, values) ⇒ Object
32 33 34 35 |
# File 'lib/rbbt/tsv/manipulate.rb', line 32 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
24 25 26 27 28 29 30 |
# File 'lib/rbbt/tsv/manipulate.rb', line 24 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
48 49 50 51 52 |
# File 'lib/rbbt/tsv/manipulate.rb', line 48 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
44 45 46 |
# File 'lib/rbbt/tsv/manipulate.rb', line 44 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 |