Permalink
Browse files

added regexp for validating postgres arrays and specs for the regexp

  • Loading branch information...
1 parent 28f91b8 commit 95ec6d13156f078a107fc0c76de0f84a482bf9f2 @fschwahn fschwahn committed Jan 23, 2012
Showing with 80 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +2 −0 .rspec
  3. +3 −0 Gemfile
  4. +13 −0 Rakefile
  5. +3 −0 activerecord-postgres-array.gemspec
  6. +3 −3 lib/activerecord-postgres-array/string.rb
  7. +5 −0 spec/spec_helper.rb
  8. +50 −0 spec/string_ext_spec.rb
View
@@ -1,3 +1,4 @@
.DS_Store
*.gem
.bundle
+Gemfile.lock
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
@@ -0,0 +1,3 @@
+source :rubygems
+
+gemspec
View
@@ -0,0 +1,13 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new
+
+task :default => :spec
@@ -16,5 +16,8 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = s.description
+
+ s.add_development_dependency 'rake'
+ s.add_development_dependency 'rspec', '~> 2.0'
end
@@ -1,5 +1,4 @@
class String
-
def to_postgres_array
self
end
@@ -9,8 +8,9 @@ def to_postgres_array
# * A string like '{10000, 10000, 10000, 10000}'
# * TODO A multi dimensional array string like '{{"meeting", "lunch"}, {"training", "presentation"}}'
def valid_postgres_array?
- # TODO validate formats above
- true
+ quoted_string_regexp = /"[^"\\]*(?:\\.[^"\\]*)*"|'[^'\\]*(?:\\.[^'\\]*)*'/
+ number_regexp = /[-+]?[0-9]*\.?[0-9]+/
+ !!match(/^\s*(\{\s*(#{number_regexp}|#{quoted_string_regexp})(\s*\,\s*(#{number_regexp}|#{quoted_string_regexp}))*\})?\s*$/)
end
# Creates an array from a postgres array string that postgresql spits out.
View
@@ -0,0 +1,5 @@
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+end
View
@@ -0,0 +1,50 @@
+require 'spec_helper'
+require 'activerecord-postgres-array/string'
+
+describe "String" do
+ describe "#valid_postgres_array?" do
+ it 'returns true for an empty string' do
+ "".should be_valid_postgres_array
+ end
+
+ it 'returns true for a string consisting only of whitespace' do
+ " ".should be_valid_postgres_array
+ end
+
+ it 'returns true for a valid postgres integer array' do
+ "{10000, 10000, 10000, 10000}".should be_valid_postgres_array
+ end
+
+ it 'returns true for a valid postgres float array' do
+ "{10000.2, .5, 10000, 10000.9}".should be_valid_postgres_array
+ end
+
+ it 'returns true for a valid postgres numerical array with irregular whitespace' do
+ "{ 10000, 10000 , 10000,10000}".should be_valid_postgres_array
+ end
+
+ it 'returns false for an array with invalid commas' do
+ "{213,}".should_not be_valid_postgres_array
+ end
+
+ it 'returns false for an array without enclosing curly brackets' do
+ "213, 1234".should_not be_valid_postgres_array
+ end
+
+ it 'returns true for a valid postgres string array' do
+ '{"ruby", "on", "rails"}'.should be_valid_postgres_array
+ end
+
+ it 'returns true for a valid postgres string array with single quotes' do
+ "{'ruby', 'on', 'rails'}".should be_valid_postgres_array
+ end
+
+ it 'returns false for string array without quotes' do
+ "{ruby, on, rails}".should_not be_valid_postgres_array
+ end
+
+ it 'returns false for concatenated strings' do
+ '{"ruby""on""rails"}'.should_not be_valid_postgres_array
+ end
+ end
+end

0 comments on commit 95ec6d1

Please sign in to comment.