lib/remi/transform.rb in remi-0.2.37 vs lib/remi/transform.rb in remi-0.2.38
- old
+ new
@@ -33,15 +33,15 @@
# Remi::Transform instances can be used interchangeably with normal lambdas.
#
# values - The values to be transformed.
#
# Returns the transformed value.
- def call(*values)
- if @multi_args
- to_proc.call(*values)
+ def call(*args)
+ if to_proc.arity == 0
+ to_proc.call
else
- to_proc.call(Array(values).first)
+ to_proc.call(*args)
end
end
# Public: Returns the transform as a lambda.
def to_proc
@@ -133,12 +133,13 @@
super
@multi_args = true
@delimiter = delimiter
end
- def transform(*values)
- Array(values).join(@delimiter)
+ def transform(row)
+ row = SourceToTargetMap::Row[row]
+ row.each_source.map { |key, value| value.blank? ? nil : value }.compact.join(@delimiter)
end
end
# Public: Transform used to do key-value lookup on hash-like objects
@@ -186,12 +187,13 @@
super
@multi_args = true
@default = default
end
- def transform(*values)
- Array(values).find(->() { @default }) { |arg| !arg.blank? }
+ def transform(row)
+ row = SourceToTargetMap::Row[row]
+ row.each_source.find(->() { [nil, @default] }) { |key, value| !value.blank? }[1]
end
end
# Public: Used to replace blank values.
#
@@ -336,11 +338,14 @@
super
@multi_args = true
@measure = measure
end
- def transform(from_date, to_date)
+ def transform(row)
+ row = SourceToTargetMap::Row[row]
+ from_date = row[row.keys[0]]
+ to_date = row[row.keys[1]]
case @measure.to_sym
when :days
(to_date - from_date).to_i
when :months
@@ -364,11 +369,11 @@
def initialize(constant, *args, **kargs, &block)
super
@constant = constant
end
- def transform(values)
+ def transform
@constant
end
end
# Public: Replaces one substring with another.
@@ -561,13 +566,14 @@
# DataFrame and checks to see if the input data matches the values
# in the columns of the sieve. Nils in the sieve are treated as
# wildcards and match anything. The first row that matches wins
# and the sieve progression stops.
#
- # sieve_df - The sieve, defined as a dataframe. The arguments
- # to the transform must appear in the same order as the
- # first N-1 columns of the sieve.
+ # sieve_df - The sieve, defined as a dataframe. The names of the
+ # sieve vectors must correspond to the names of the
+ # vectors in the dataframe source to target map. The
+ # last vector in the sieve_df is used as the result of the sieve.
#
#
# Examples:
#
# # This sieve captures the following business logic
@@ -610,27 +616,30 @@
# 3 Undergrad NURS nil intensive
# 4 Unknown CHEM true base
class DataFrameSieve < Transform
def initialize(sieve_df, *args, **kargs, &block)
super
- @sieve_df = sieve_df.transpose.to_h.values
+ @sieve_table = sieve_df.transpose.to_h.values
end
- def transform(*values)
- sieve_keys = @sieve_df.first.index.to_a
+
+ def transform(row)
+ sieve_keys = @sieve_table.first.index.to_a
sieve_result_key = sieve_keys.pop
- @sieve_df.each.find do |sieve_row|
+ raise ArgumentError, "#{sieve_keys - row.source_keys} not found in row" unless (sieve_keys - row.source_keys).size == 0
+
+ @sieve_table.each.find do |sieve_row|
match_row = true
- sieve_keys.each_with_index do |key,idx|
- match_value = if sieve_row[key].is_a?(Regexp)
- !!sieve_row[key].match(values[idx])
- else
- sieve_row[key] == values[idx]
- end
+ sieve_keys.each do |sieve_key|
+ match_value = if sieve_row[sieve_key].is_a?(Regexp)
+ !!sieve_row[sieve_key].match(row[sieve_key])
+ else
+ sieve_row[sieve_key] == row[sieve_key]
+ end
- match_row &&= sieve_row[key].nil? || match_value
+ match_row &&= sieve_row[sieve_key].nil? || match_value
end
match_row
end[sieve_result_key]
end
end
@@ -659,10 +668,10 @@
end
attr_reader :buckets
attr_reader :current_population
- def transform(*values)
+ def transform
get_next_value
end
def size
@size ||= @current_population.reduce(0) { |sum, (group, n)| sum += n }