Permalink
Browse files

Initial Commit

  • Loading branch information...
0 parents commit 1ff152d6206726c7f03e291d749b2531fde9da3f @mattfordham committed Jun 5, 2012
@@ -0,0 +1,4 @@
+.bundle/
+log/*.log
+pkg/
+.DS_Store
@@ -0,0 +1,3 @@
+source "http://rubygems.org"
+
+gemspec
@@ -0,0 +1,101 @@
+PATH
+ remote: .
+ specs:
+ csv_validator (0.0.1)
+ activemodel
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionpack (3.2.3)
+ activemodel (= 3.2.3)
+ activesupport (= 3.2.3)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.1)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.2)
+ activemodel (3.2.3)
+ activesupport (= 3.2.3)
+ builder (~> 3.0.0)
+ activesupport (3.2.3)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ archive-tar-minitar (0.5.2)
+ builder (3.0.0)
+ columnize (0.3.6)
+ diff-lcs (1.1.3)
+ erubis (2.7.0)
+ ffi (1.0.11)
+ guard (1.0.3)
+ ffi (>= 0.5.0)
+ thor (>= 0.14.6)
+ guard-rspec (0.7.3)
+ guard (>= 0.10.0)
+ hike (1.2.1)
+ i18n (0.6.0)
+ journey (1.0.3)
+ json (1.7.3)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
+ multi_json (1.3.2)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ railties (3.2.3)
+ actionpack (= 3.2.3)
+ activesupport (= 3.2.3)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ rspec (2.9.0)
+ rspec-core (~> 2.9.0)
+ rspec-expectations (~> 2.9.0)
+ rspec-mocks (~> 2.9.0)
+ rspec-core (2.9.0)
+ rspec-expectations (2.9.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.9.0)
+ rspec-rails (2.9.0)
+ actionpack (>= 3.0)
+ activesupport (>= 3.0)
+ railties (>= 3.0)
+ rspec (~> 2.9.0)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
+ sprockets (2.1.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ thor (0.14.6)
+ tilt (1.3.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ csv_validator!
+ guard
+ guard-rspec
+ rake
+ rspec
+ rspec-rails
+ ruby-debug19
@@ -0,0 +1,7 @@
+guard 'rspec', :cli => "-d --color", :version => 2 do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
+ watch(%r{^lib/csv_validator/csv_validator.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
+ watch('spec/spec_helper.rb') { "spec" }
+end
+
@@ -0,0 +1,20 @@
+Copyright 2012 YOURNAME
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
+= CsvValidator
+
+This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,27 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+begin
+ require 'rdoc/task'
+rescue LoadError
+ require 'rdoc/rdoc'
+ require 'rake/rdoctask'
+ RDoc::Task = Rake::RDocTask
+end
+
+RDoc::Task.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'CsvValidator'
+ rdoc.options << '--line-numbers'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+
+
+
+Bundler::GemHelper.install_tasks
+
@@ -0,0 +1,24 @@
+$:.push File.expand_path("../lib", __FILE__)
+
+# Describe your gem and declare its dependencies:
+Gem::Specification.new do |s|
+ s.name = "csv_validator"
+ s.version = "0.0.1"
+ s.authors = ["TODO: Your name"]
+ s.email = ["TODO: Your email"]
+ s.homepage = "TODO"
+ s.summary = "TODO: Summary of CsvValidator."
+ s.description = "TODO: Description of CsvValidator."
+
+ s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
+
+ s.require_paths = %w(lib)
+ s.add_dependency("activemodel", ">= 0")
+ s.add_development_dependency("rake")
+ s.add_development_dependency("rspec", ">= 0")
+ s.add_development_dependency("rspec-rails")
+ s.add_development_dependency("ruby-debug19")
+ s.add_development_dependency("guard")
+ s.add_development_dependency("guard-rspec")
+
+end
@@ -0,0 +1,99 @@
+require 'csv'
+
+class CsvValidator < ActiveModel::EachValidator
+ @@default_options = {}
+
+ def self.default_options
+ @@default_options
+ end
+
+ def validate_each(record, attribute, value)
+ options = @@default_options.merge(self.options)
+
+ begin
+ csv = CSV.read(value)
+ rescue CSV::MalformedCSVError
+ record.errors.add(attribute, options[:message] || "is not a valid CSV file")
+ return
+ end
+
+ if options[:columns]
+ unless csv[0].length == options[:columns]
+ record.errors.add(attribute, options[:message] || "should have #{options[:columns]} columns")
+ end
+ end
+
+ if options[:max_columns]
+ if csv[0].length > options[:max_columns]
+ record.errors.add(attribute, options[:message] || "should have no more than #{options[:max_columns]} columns")
+ end
+ end
+
+ if options[:min_columns]
+ if csv[0].length < options[:min_columns]
+ record.errors.add(attribute, options[:message] || "should have at least #{options[:min_columns]} columns")
+ end
+ end
+
+ if options[:rows]
+ unless csv.length == options[:rows]
+ record.errors.add(attribute, options[:message] || "should have #{options[:rows]} rows")
+ end
+ end
+
+ if options[:min_rows]
+ if csv.length < options[:min_rows]
+ record.errors.add(attribute, options[:message] || "should have at least #{options[:min_rows]} rows")
+ end
+ end
+
+ if options[:max_rows]
+ if csv.length > options[:max_rows]
+ record.errors.add(attribute, options[:message] || "should have no more than #{options[:max_rows]} rows")
+ end
+ end
+
+ if options[:email]
+ emails = column_to_array(csv, options[:email])
+ invalid_emails = []
+ emails.each do |email|
+ unless email.match /\A[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
+ invalid_emails << email
+ end
+ end
+ if invalid_emails.length > 0
+ record.errors.add(attribute, options[:message] || "contains invalid emails (#{invalid_emails.join(', ')})")
+ end
+ end
+
+ if options[:numericality]
+ numbers = column_to_array(csv, options[:numericality])
+ numbers.each do |number|
+ unless is_numeric?(number)
+ record.errors.add(attribute, options[:message] || "contains non-numeric content in column #{options[:numericality]}")
+ return
+ end
+ end
+ end
+
+ end
+
+ private
+
+ def column_to_array(csv, column_index)
+ column_contents = []
+ csv.each do |column|
+ column_contents << column[column_index].strip
+ end
+ column_contents
+ end
+
+ def is_numeric?(string)
+ Float(string)
+ true
+ rescue
+ false
+ end
+
+
+end
Oops, something went wrong.

0 comments on commit 1ff152d

Please sign in to comment.