Skip to content

Commit

Permalink
added regexp for validating postgres arrays and specs for the regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
fschwahn committed Jan 23, 2012
1 parent 28f91b8 commit 95ec6d1
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.gem
.bundle
Gemfile.lock
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source :rubygems

gemspec
13 changes: 13 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions activerecord-postgres-array.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

6 changes: 3 additions & 3 deletions lib/activerecord-postgres-array/string.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class String

def to_postgres_array
self
end
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions spec/string_ext_spec.rb
Original file line number Diff line number Diff line change
@@ -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.