Skip to content

Commit

Permalink
Changes - Not been very good at maintaining this
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtimberlake committed May 12, 2010
1 parent 3b0de02 commit f74b27b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 54 deletions.
3 changes: 0 additions & 3 deletions History.txt

This file was deleted.

46 changes: 12 additions & 34 deletions Rakefile
@@ -1,38 +1,16 @@
# Look in the tasks/setup.rb file for the various options that can be
# configured in this Rakefile. The .rake files in the tasks directory
# are where the options are used.
require 'rubygems'
require 'rake'

begin
require 'bones'
Bones.setup
rescue LoadError
begin
load 'tasks/setup.rb'
rescue LoadError
raise RuntimeError, '### please install the "bones" gem ###'
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "map_fields"
gem.summary = %Q{Rails plugin to allow a user to map the fields of a CSV to an expected list of fields}
gem.email = "andrew@andrewtimberlake.com"
gem.homepage = "http://github.com/andrewtimberlake/map-fields"
gem.authors = ["Andrew Timberlake"]
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end

ensure_in_path 'lib'
require 'map_fields'

task :default => 'spec:run'

PROJ.name = 'map-fields'
PROJ.authors = 'Andrew Timberlake'
PROJ.email = 'andrew@andrewtimberlake.com'
PROJ.url = 'http://github.com/internuity/map-fields'
PROJ.version = MapFields::VERSION
PROJ.rubyforge.name = 'internuity'
PROJ.readme_file = 'README.rdoc'
PROJ.gem.files = FileList['lib/**/**', 'views/**/**', 'README.rdoc', 'init.rb', 'History.txt']

PROJ.exclude = %w(spec/rails_root)

PROJ.rdoc.remote_dir = 'map-fields'

PROJ.spec.opts << '--color'

depend_on 'fastercsv'

# EOF
67 changes: 50 additions & 17 deletions lib/map_fields.rb
Expand Up @@ -16,7 +16,9 @@ def map_fields
self.class.read_inheritable_attribute(:map_fields_options)
)

if session[:map_fields].nil? || params[options[:file_field]]
RAILS_DEFAULT_LOGGER.debug("session[:map_fields]: #{session[:map_fields]}")
RAILS_DEFAULT_LOGGER.debug("params[options[:file_field]]: #{params[options[:file_field]]}")
if session[:map_fields].nil? || !params[options[:file_field]].blank?
session[:map_fields] = {}
if params[options[:file_field]].blank?
@map_fields_error = MissingFileContentsError
Expand All @@ -31,28 +33,40 @@ def map_fields
end

session[:map_fields][:file] = temp_path

@rows = []
FasterCSV.foreach(temp_path) do |row|
@rows << row
break if @rows.size == 10
end
expected_fields = self.class.read_inheritable_attribute(:map_fields_fields)
@fields = ([nil] + expected_fields).inject([]){ |o, e| o << [e, o.size]}
@parameters = []
options[:params].each do |param|
@parameters += ParamsParser.parse(params, param)
end
else
if session[:map_fields][:file].nil? || params[:fields].nil?
session[:map_fields] = nil
@map_fields_error = InconsistentStateError
else
expected_fields = self.class.read_inheritable_attribute(:map_fields_fields)
if expected_fields.respond_to?(:call)
expected_fields = expected_fields.call(params)
end
@mapped_fields = MappedFields.new(session[:map_fields][:file],
expected_fields,
params[:fields],
params[:ignore_first_row])
end
end

@rows = []
begin
FasterCSV.foreach(session[:map_fields][:file]) do |row|
@rows << row
break if @rows.size == 10
end
rescue FasterCSV::MalformedCSVError => e
@map_fields_error = e
end
expected_fields = self.class.read_inheritable_attribute(:map_fields_fields)
if expected_fields.respond_to?(:call)
expected_fields = expected_fields.call(params)
end
@fields = ([nil] + expected_fields).inject([]){ |o, e| o << [e, o.size]}
@parameters = []
options[:params].each do |param|
@parameters += ParamsParser.parse(params, param)
end
end

def mapped_fields
Expand Down Expand Up @@ -81,28 +95,47 @@ def map_fields_cleanup

module ClassMethods
def map_fields(action, fields, options = {})
write_inheritable_array(:map_fields_fields, fields)
write_inheritable_attribute(:map_fields_fields, fields)
write_inheritable_attribute(:map_fields_options, options)
before_filter :map_fields, :only => action
after_filter :map_fields_cleanup, :only => action
end
end

class MappedFields
def initialize(file, mapping, ignore_first_row)
attr_reader :mapping, :ignore_first_row, :file

def initialize(file, fields, mapping, ignore_first_row)
@file = file
@fields = fields
@mapping = {}
@ignore_first_row = ignore_first_row

mapping.each do |k,v|
@mapping[v.to_i - 1] = k.to_i - 1 unless v.to_i == 0
unless v.to_i == 0
#Numeric mapping
@mapping[v.to_i - 1] = k.to_i - 1
#Text mapping
@mapping[fields[v.to_i-1]] = k.to_i - 1
#Symbol mapping
sym_key = fields[v.to_i-1].downcase.
gsub(/[-\s\/]+/, '_').
gsub(/[^a-zA-Z0-9_]+/, '').
to_sym
@mapping[sym_key] = k.to_i - 1
end
end
end

def is_mapped?(field)
!@mapping[field].nil?
end

def each
row_number = 1
FasterCSV.foreach(@file) do |csv_row|
unless row_number == 1 && @ignore_first_row
row = []
row = {}
@mapping.each do |k,v|
row[k] = csv_row[v]
end
Expand Down

0 comments on commit f74b27b

Please sign in to comment.