lib/tanemaki.rb in tanemaki-0.1.0 vs lib/tanemaki.rb in tanemaki-0.1.1
- old
+ new
@@ -12,10 +12,15 @@
Seeder.(Parser.(path), {eval_scope: @eval_scope}.merge(options))
end
def default_eval_scope(eval_scope)
+ self.default_eval_scope = eval_scope
+ end
+
+
+ def default_eval_scope=(eval_scope)
@eval_scope = eval_scope
end
end
@@ -26,15 +31,29 @@
end
def ready(path)
header, *lines = CSV.read(path)
- header.map!(&:to_sym)
+ nameless = []
+ readiness = header.map.with_index do |name, index|
+ if name
+ name.to_sym
+ else
+ nameless.push(index)
+ nil
+ end
+ end
+
lines.map do |line|
+ nameless_parameter = []
line.each_with_index.each_with_object({}) do |(col, index), result|
- result[header[index]] = col if col
- end
+ if nameless.include?(index)
+ nameless_parameter.push(col) if col
+ else
+ result[readiness[index]] = col if col
+ end
+ end.merge(namelass_parameter_array: nameless_parameter)
end
end
end
end
@@ -69,17 +88,21 @@
end
def seed(klass = nil, method = nil, &block)
@named_csv.map do |row|
- readiness = evaluated(row)
+ readiness, nameless = begin
+ result = evaluated(row.dup)
+ [result, result.delete(:namelass_parameter_array)]
+ end
+
begin
- (klass || @klass).send((method || @method), **readiness)
+ (klass || @klass).send((method || @method), *nameless, **readiness)
rescue => e
raise e unless block_given?
- block.(row, e)
+ block.(e, row)
nil
end
end.compact
end
@@ -95,22 +118,31 @@
private
def evaluated(row)
return row if @evaluate.size == 0
- row.each_pair.each_with_object({}) do |(k, v), result|
- next result[k] = v unless @evaluate.include?(k)
+ row[:namelass_parameter_array].map!.with_index do |value, index|
+ next value unless @evaluate.include?(index)
- result[k] = begin
- return eval(v) unless @eval_scope
+ do_eval(value)
+ end
- @eval_scope.instance_eval do
- eval(v)
- end
- rescue
- v
- end
+ row.each_pair.each_with_object({}) do |(key, value), result|
+ next result[key] = value unless @evaluate.include?(key)
+
+ result[key] = do_eval(value)
end
+ end
+
+
+ def do_eval(value)
+ return eval(value) unless @eval_scope
+
+ @eval_scope.instance_eval do
+ eval(value)
+ end
+ rescue
+ value
end
def for_chain
{