lib/rbbt/tsv/manipulate.rb in rbbt-util-4.4.0 vs lib/rbbt/tsv/manipulate.rb in rbbt-util-5.0.0
- old
+ new
@@ -179,11 +179,18 @@
# Annotated with Entity and NamedArray
if not @unnamed
if not traverser.new_field_names.nil?
case type
when :double, :list
- NamedArray.setup value, traverser.new_field_names, key, entity_options
+ if value.frozen?
+ Log.warn "Value frozen: #{ value }"
+ end
+ if value.nil?
+ nil
+ else
+ NamedArray.setup value, traverser.new_field_names, key, entity_options
+ end
when :flat, :single
Misc.prepare_entity(value, traverser.new_field_names.first, entity_options)
end
end
end
@@ -278,11 +285,11 @@
end
elems.sort_by{|k,v| v}.collect{|k,v| k}
end
- def select(method = nil)
+ def select(method = nil, &block)
new = TSV.setup({}, :key_field => key_field, :fields => fields, :type => type, :filename => filename, :identifiers => identifiers)
new.key_field = key_field
new.fields = fields.dup unless fields.nil?
new.type = type
@@ -318,28 +325,52 @@
new[key] = values if [key,values].flatten.select{|v| v =~ method}.any?
end
end
when String === method
if block_given?
- with_unnamed do
- case
- when (method == key_field or method == :key)
- through do |key, values|
- new[key] = values if yield(key)
+ case
+ when block.arity == 1
+ with_unnamed do
+ case
+ when (method == key_field or method == :key)
+ through do |key, values|
+ new[key] = values if yield(key)
+ end
+ when (type == :single or type == :flat)
+ through do |key, value|
+ new[key] = value if yield(value)
+ end
+ else
+ pos = identify_field method
+ through do |key, values|
+ new[key] = values if yield(values[pos])
+ end
end
- when (type == :single or type == :flat)
- through do |key, value|
- new[key] = value if yield(value)
+ end
+ when block.arity == 2
+ with_unnamed do
+ case
+ when (method == key_field or method == :key)
+ through do |key, values|
+ new[key] = values if yield(key, key)
+ end
+ when (type == :single or type == :flat)
+ through do |key, value|
+ new[key] = value if yield(key, value)
+ end
+ else
+ pos = identify_field method
+ through do |key, values|
+ new[key] = values if yield(key, values[pos])
+ end
end
- else
- pos = identify_field method
- through do |key, values|
- new[key] = values if yield(values[pos])
- end
+
end
end
+
+
else
with_unnamed do
through do |key, values|
new[key] = values if [key,values].flatten.select{|v| v == method}.any?
end
@@ -403,10 +434,26 @@
end
new
end
+ def column(field, cast = nil)
+ new = slice(field)
+
+ new.with_unnamed do
+ new.each do |k,v|
+ nv = v.first
+ nv = nv.send(cast) unless cast.nil?
+ new[k] = nv
+ end
+ end
+
+ new.type = :single
+
+ new
+ end
+
def process(field, &block)
field_pos = identify_field field
through do |key, values|
next if values.nil?
@@ -436,13 +483,19 @@
when type == :single
self[key] = new_values
when type == :flat
self[key] = new_values
else
- values[field_pos].replace new_values
+ if (String === values[field_pos] and String === new_values) or
+ (Array === values[field_pos] and Array === new_values)
+ values[field_pos].replace new_values
+ else
+ values[field_pos] = new_values
+ end
self[key] = values
end
end
+ self
end
def add_field(name = nil)
old_monitor = @monitor
@monitor = {:desc => "Adding field #{ name }"} if TrueClass === monitor