Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add StringArray type.

  • Loading branch information...
commit c5010871d2dffbb5088df108a23b6d4ab677e151 1 parent 8aaede9
@goodwink goodwink authored
View
12 lib/dm-pg-types.rb
@@ -7,17 +7,22 @@ module DataMapper
class Property
autoload :HStore, 'dm-pg-types/hstore'
autoload :DecimalArray, 'dm-pg-types/decimal_array'
+ autoload :StringArray, 'dm-pg-types/string_array'
end
module Migrations
module PostgresAdapter
def property_schema_hash(property)
schema = super
-
+
if property.kind_of?(Property::DecimalArray)
schema[:primitive] = "#{schema[:primitive]}(#{property.precision},#{property.scale})[]"
schema[:precision] = schema[:scale] = nil
+ elsif property.kind_of?(Property::StringArray)
+ schema[:primitive] = "#{schema[:primitive]}(#{property.length})[]"
+ schema[:length] = nil
end
+
schema
end
end
@@ -27,7 +32,7 @@ module PgTypes
def self.included(base)
base.extend ClassMethods
end
-
+
module ClassMethods
# Types for PostgreSQL databases.
#
@@ -37,7 +42,8 @@ module ClassMethods
def type_map
super.merge(
Property::HStore => {:primitive => 'HSTORE'},
- Property::DecimalArray => {:primitive => "NUMERIC"}
+ Property::DecimalArray => {:primitive => "NUMERIC"},
+ Property::StringArray => {:primitive => "VARCHAR"}
).freeze
end
end
View
35 lib/dm-pg-types/string_array.rb
@@ -0,0 +1,35 @@
+require 'dm-core'
+require 'csv'
+
+module DataMapper
+ class Property
+ class StringArray < Object
+ accept_options :length
+
+ DEFAULT_LENGTH = 50
+ length(DEFAULT_LENGTH)
+
+ def length
+ if @length.kind_of?(Range)
+ @length.max
+ else
+ @length
+ end
+ end
+
+ def initialize(model, name, options = {})
+ super
+ @length = @options.fetch(:length)
+ end
+
+
+ def load(value)
+ CSV.parse_line(value.gsub(/[{}]/,'')) || [] if value
+ end
+
+ def dump(value)
+ "{#{CSV.generate_line(value, :row_sep => '')}}" if value
+ end
+ end # class StringArray
+ end # class Property
+end # module DataMapper
View
1  spec/fixtures/person.rb
@@ -15,6 +15,7 @@ class Person
property :name, String
property :other_information, HStore
property :numbers, DecimalArray
+ property :aliases, StringArray
end
end
end
View
6 spec/integration/full_spec.rb
@@ -13,6 +13,7 @@ class Person
property :name, String
property :info, HStore
property :decimals, DecimalArray, :scale => 5, :precision => 10
+ property :aliases, StringArray, :length => 10
end
DataMapper.finalize
@@ -21,6 +22,7 @@ class Person
p = Person.new
p.info = {:a => "b", :c => "d"}
p.decimals = [10.1, 11.2]
+ p.aliases = ["Bob", "George"]
p.save
@p = Person.last
end
@@ -33,4 +35,8 @@ class Person
@p.decimals.should == [10.1,11.2]
end
+ it "should provide a person with aliases" do
+ @p.aliases.should == ['Bob', 'George']
+ end
+
end
View
67 spec/integration/string_array_spec.rb
@@ -0,0 +1,67 @@
+require 'spec_helper'
+
+try_spec do
+
+ require './spec/fixtures/person'
+
+ describe DataMapper::TypesFixtures::Person do
+ supported_by :all do
+ before :all do
+ @resource = DataMapper::TypesFixtures::Person.new(:name => '')
+ end
+
+ describe 'with no other information' do
+ before :all do
+ @resource.aliases = nil
+ end
+
+ describe 'when dumped and loaded again' do
+ before :all do
+ @resource.save.should be(true)
+ @resource.reload
+ end
+
+ it 'has no aliases' do
+ @resource.aliases.should == nil
+ end
+ end
+ end
+
+ describe 'with no aliases information' do
+ before :all do
+ @resource.aliases = []
+ end
+
+ describe 'when dumped and loaded again' do
+ before :all do
+ @resource.save.should be(true)
+ @resource.reload
+ end
+
+ it 'has empty aliases list' do
+ @resource.aliases.should == []
+ end
+ end
+ end
+
+ describe 'with a few items on the aliases list' do
+ before :all do
+ @input = ['Bob','George','Michael']
+ @resource.aliases = @input
+ end
+
+ describe 'when dumped and loaded again' do
+ before :all do
+ @resource.save.should be(true)
+ @resource.reload
+ end
+
+ it 'includes saved aliases' do
+ @resource.aliases.should == ['Bob','George','Michael']
+ end
+
+ end
+ end
+ end
+ end
+end
View
0  spec/unit/pg_array_spec.rb → spec/unit/decimal_array_spec.rb
File renamed without changes
View
43 spec/unit/string_array_spec.rb
@@ -0,0 +1,43 @@
+require 'spec_helper'
+
+try_spec do
+ describe DataMapper::Property::StringArray do
+ supported_by :all do
+ before :all do
+ class User
+ include DataMapper::Resource
+ property :id, Serial
+ property :things, StringArray, :length => 10
+ end
+
+ @property = User.properties[:things]
+ end
+
+ describe '.load' do
+ describe "argument is a string that is a string array" do
+ before :all do
+ @input = '{"10.1","2.3","4","5","6"}'
+ @result = @property.load(@input)
+ end
+
+ it "parses the input into an array" do
+ @result.should == ['10.1','2.3','4','5','6']
+ end
+ end
+ end
+
+ describe ".dump" do
+ before :all do
+ @input = ['foo','bar','baz','quux','foo,bar']
+ @result = @property.dump(@input)
+ end
+
+ describe "when argument is an Array" do
+ it "should result in a postgres array string" do
+ @result.should == '{foo,bar,baz,quux,"foo,bar"}'
+ end
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.