dm-pg-types 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -2
- data/Gemfile.lock +11 -11
- data/README.md +14 -4
- data/Rakefile +1 -1
- data/dm-pg-types.gemspec +9 -5
- data/lib/dm-pg-types.rb +10 -3
- data/lib/dm-pg-types/decimal_array.rb +2 -6
- data/lib/dm-pg-types/pg_array.rb +16 -0
- data/lib/dm-pg-types/string_array.rb +25 -0
- data/spec/fixtures/person.rb +1 -0
- data/spec/integration/decimal_array_spec.rb +0 -2
- data/spec/integration/full_spec.rb +11 -5
- data/spec/integration/string_array_spec.rb +67 -0
- data/spec/unit/{pg_array_spec.rb → decimal_array_spec.rb} +0 -0
- data/spec/unit/string_array_spec.rb +43 -0
- metadata +10 -6
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -15,14 +15,14 @@ GEM
|
|
15
15
|
dm-postgres-adapter (1.2.0)
|
16
16
|
dm-do-adapter (~> 1.2.0)
|
17
17
|
do_postgres (~> 0.10.6)
|
18
|
-
dm-types (1.2.
|
19
|
-
bcrypt-ruby (~> 3.0
|
18
|
+
dm-types (1.2.2)
|
19
|
+
bcrypt-ruby (~> 3.0)
|
20
20
|
dm-core (~> 1.2.0)
|
21
|
-
fastercsv (~> 1.5
|
22
|
-
json (~> 1.6
|
23
|
-
multi_json (~> 1.0
|
24
|
-
stringex (~> 1.
|
25
|
-
uuidtools (~> 2.1
|
21
|
+
fastercsv (~> 1.5)
|
22
|
+
json (~> 1.6)
|
23
|
+
multi_json (~> 1.0)
|
24
|
+
stringex (~> 1.4)
|
25
|
+
uuidtools (~> 2.1)
|
26
26
|
dm-validations (1.2.0)
|
27
27
|
dm-core (~> 1.2.0)
|
28
28
|
do_postgres (0.10.8)
|
@@ -34,14 +34,14 @@ GEM
|
|
34
34
|
git (>= 1.2.5)
|
35
35
|
rake
|
36
36
|
rdoc
|
37
|
-
json (1.
|
38
|
-
multi_json (1.
|
37
|
+
json (1.7.5)
|
38
|
+
multi_json (1.3.6)
|
39
39
|
rake (0.9.2.2)
|
40
40
|
rdoc (3.12)
|
41
41
|
json (~> 1.4)
|
42
42
|
rspec (1.3.2)
|
43
|
-
stringex (1.
|
44
|
-
uuidtools (2.1.
|
43
|
+
stringex (1.4.0)
|
44
|
+
uuidtools (2.1.3)
|
45
45
|
|
46
46
|
PLATFORMS
|
47
47
|
ruby
|
data/README.md
CHANGED
@@ -1,24 +1,34 @@
|
|
1
1
|
# dm-pg-types
|
2
2
|
|
3
|
+
# Installation
|
4
|
+
|
5
|
+
dm-pg-types is available as a RubyGem. Simply put this in your Gemfile
|
6
|
+
|
7
|
+
```
|
8
|
+
gem 'dm-pg-types'
|
9
|
+
```
|
10
|
+
|
11
|
+
and you should be good to go.
|
12
|
+
|
3
13
|
DataMapper plugin providing support for PostgreSQL's HSTORE and ARRAY types. An example will suffice
|
4
14
|
|
5
15
|
```ruby
|
6
|
-
DataMapper.setup(:default, 'postgres://
|
16
|
+
DataMapper.setup(:default, 'postgres://localhost/dm_pg_types_person')
|
7
17
|
DataMapper.repository(:default).adapter.execute("DROP TABLE IF EXISTS people")
|
8
18
|
DataMapper.repository(:default).adapter.execute("CREATE EXTENSION HSTORE")
|
9
19
|
|
10
20
|
class Person
|
11
21
|
include DataMapper::Resource
|
12
|
-
|
22
|
+
|
13
23
|
property :id, Serial
|
14
24
|
property :name, String
|
15
25
|
property :info, HStore
|
16
26
|
property :decimals, DecimalArray, :scale => 5, :precision => 10
|
17
27
|
end
|
18
|
-
|
28
|
+
|
19
29
|
DataMapper.finalize
|
20
30
|
DataMapper.auto_migrate!
|
21
|
-
|
31
|
+
|
22
32
|
p = Person.new
|
23
33
|
p.info = {:a => "b", :c => "d"}
|
24
34
|
p.decimals = [10.1, 11.2]
|
data/Rakefile
CHANGED
data/dm-pg-types.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "dm-pg-types"
|
8
|
-
s.version = "0.8.
|
8
|
+
s.version = "0.8.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["svs"]
|
12
|
-
s.date = "2012-09-
|
12
|
+
s.date = "2012-09-19"
|
13
13
|
s.description = "DataMapper plugin providing HSTORE and ARRAY datatype support for postgres"
|
14
14
|
s.email = "svs [a] svs [d] io"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,17 +28,21 @@ Gem::Specification.new do |s|
|
|
28
28
|
"lib/dm-pg-types.rb",
|
29
29
|
"lib/dm-pg-types/decimal_array.rb",
|
30
30
|
"lib/dm-pg-types/hstore.rb",
|
31
|
+
"lib/dm-pg-types/pg_array.rb",
|
32
|
+
"lib/dm-pg-types/string_array.rb",
|
31
33
|
"spec/fixtures/person.rb",
|
32
34
|
"spec/integration/decimal_array_spec.rb",
|
33
35
|
"spec/integration/full_spec.rb",
|
34
36
|
"spec/integration/hstore_spec.rb",
|
37
|
+
"spec/integration/string_array_spec.rb",
|
35
38
|
"spec/rcov.opts",
|
36
39
|
"spec/shared/flags_shared_spec.rb",
|
37
40
|
"spec/shared/identity_function_group.rb",
|
38
41
|
"spec/spec.opts",
|
39
42
|
"spec/spec_helper.rb",
|
43
|
+
"spec/unit/decimal_array_spec.rb",
|
40
44
|
"spec/unit/hstore_spec.rb",
|
41
|
-
"spec/unit/
|
45
|
+
"spec/unit/string_array_spec.rb",
|
42
46
|
"tasks/spec.rake",
|
43
47
|
"tasks/yard.rake",
|
44
48
|
"tasks/yardstick.rake"
|
@@ -62,8 +66,8 @@ Gem::Specification.new do |s|
|
|
62
66
|
s.add_runtime_dependency(%q<dm-do-adapter>, ["~> 1.2.0"])
|
63
67
|
s.add_runtime_dependency(%q<dm-postgres-adapter>, ["~> 1.2.0"])
|
64
68
|
s.add_runtime_dependency(%q<rake>, ["~> 0.9.2"])
|
65
|
-
s.
|
66
|
-
s.
|
69
|
+
s.add_development_dependency(%q<rspec>, ["~> 1.3.2"])
|
70
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
|
67
71
|
s.add_runtime_dependency(%q<data_mapper>, ["~> 1.2.0"])
|
68
72
|
s.add_runtime_dependency(%q<dm-postgres-adapter>, ["~> 1.2.0"])
|
69
73
|
else
|
data/lib/dm-pg-types.rb
CHANGED
@@ -5,19 +5,25 @@ require 'dm-postgres-adapter'
|
|
5
5
|
|
6
6
|
module DataMapper
|
7
7
|
class Property
|
8
|
+
autoload :PgArray, 'dm-pg-types/pg_array'
|
8
9
|
autoload :HStore, 'dm-pg-types/hstore'
|
9
10
|
autoload :DecimalArray, 'dm-pg-types/decimal_array'
|
11
|
+
autoload :StringArray, 'dm-pg-types/string_array'
|
10
12
|
end
|
11
13
|
|
12
14
|
module Migrations
|
13
15
|
module PostgresAdapter
|
14
16
|
def property_schema_hash(property)
|
15
17
|
schema = super
|
16
|
-
|
18
|
+
|
17
19
|
if property.kind_of?(Property::DecimalArray)
|
18
20
|
schema[:primitive] = "#{schema[:primitive]}(#{property.precision},#{property.scale})[]"
|
19
21
|
schema[:precision] = schema[:scale] = nil
|
22
|
+
elsif property.kind_of?(Property::StringArray)
|
23
|
+
schema[:primitive] = "#{schema[:primitive]}(#{property.length})[]"
|
24
|
+
schema[:length] = nil
|
20
25
|
end
|
26
|
+
|
21
27
|
schema
|
22
28
|
end
|
23
29
|
end
|
@@ -27,7 +33,7 @@ module DataMapper
|
|
27
33
|
def self.included(base)
|
28
34
|
base.extend ClassMethods
|
29
35
|
end
|
30
|
-
|
36
|
+
|
31
37
|
module ClassMethods
|
32
38
|
# Types for PostgreSQL databases.
|
33
39
|
#
|
@@ -37,7 +43,8 @@ module DataMapper
|
|
37
43
|
def type_map
|
38
44
|
super.merge(
|
39
45
|
Property::HStore => {:primitive => 'HSTORE'},
|
40
|
-
Property::DecimalArray => {:primitive => "NUMERIC"}
|
46
|
+
Property::DecimalArray => {:primitive => "NUMERIC"},
|
47
|
+
Property::StringArray => {:primitive => "VARCHAR"}
|
41
48
|
).freeze
|
42
49
|
end
|
43
50
|
end
|
@@ -2,7 +2,7 @@ require 'dm-core'
|
|
2
2
|
|
3
3
|
module DataMapper
|
4
4
|
class Property
|
5
|
-
class DecimalArray <
|
5
|
+
class DecimalArray < PgArray
|
6
6
|
accept_options :precision, :scale, :min, :max
|
7
7
|
attr_reader :precision, :scale, :min, :max
|
8
8
|
|
@@ -18,11 +18,7 @@ module DataMapper
|
|
18
18
|
|
19
19
|
|
20
20
|
def load(value)
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
def dump(value)
|
25
|
-
"{#{value.join(",")}}" if value
|
21
|
+
super.map(&:to_f) if value
|
26
22
|
end
|
27
23
|
end # class DecimalArray
|
28
24
|
end # class Property
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'dm-core'
|
2
|
+
require 'csv'
|
3
|
+
|
4
|
+
module DataMapper
|
5
|
+
class Property
|
6
|
+
class PgArray < Object
|
7
|
+
def load(value)
|
8
|
+
CSV.parse_line(value.gsub(/[{}]/,'')) || [] if value
|
9
|
+
end
|
10
|
+
|
11
|
+
def dump(value)
|
12
|
+
"{#{CSV.generate_line(value, :row_sep => '')}}" if value
|
13
|
+
end
|
14
|
+
end # class PgArray
|
15
|
+
end # class Property
|
16
|
+
end # module DataMapper
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'dm-core'
|
2
|
+
|
3
|
+
module DataMapper
|
4
|
+
class Property
|
5
|
+
class StringArray < PgArray
|
6
|
+
accept_options :length
|
7
|
+
|
8
|
+
DEFAULT_LENGTH = 50
|
9
|
+
length(DEFAULT_LENGTH)
|
10
|
+
|
11
|
+
def length
|
12
|
+
if @length.kind_of?(Range)
|
13
|
+
@length.max
|
14
|
+
else
|
15
|
+
@length
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(model, name, options = {})
|
20
|
+
super
|
21
|
+
@length = @options.fetch(:length)
|
22
|
+
end
|
23
|
+
end # class StringArray
|
24
|
+
end # class Property
|
25
|
+
end # module DataMapper
|
data/spec/fixtures/person.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
require 'debugger'
|
4
3
|
try_spec do
|
5
4
|
|
6
5
|
require './spec/fixtures/person'
|
@@ -35,7 +34,6 @@ try_spec do
|
|
35
34
|
|
36
35
|
describe 'when dumped and loaded again' do
|
37
36
|
before :all do
|
38
|
-
debugger
|
39
37
|
@resource.save.should be(true)
|
40
38
|
@resource.reload
|
41
39
|
end
|
@@ -3,28 +3,30 @@ require 'spec_helper'
|
|
3
3
|
describe "dm-pg-types" do
|
4
4
|
before :all do
|
5
5
|
# Create the table manually before running this spec.
|
6
|
-
DataMapper.setup(:default, 'postgres://
|
6
|
+
DataMapper.setup(:default, 'postgres://localhost/dm_pg_types_person')
|
7
7
|
DataMapper.repository(:default).adapter.execute("DROP TABLE IF EXISTS people")
|
8
8
|
DataMapper.repository(:default).adapter.execute("CREATE EXTENSION HSTORE") rescue nil
|
9
9
|
class Person
|
10
10
|
include DataMapper::Resource
|
11
|
-
|
11
|
+
|
12
12
|
property :id, Serial
|
13
13
|
property :name, String
|
14
14
|
property :info, HStore
|
15
15
|
property :decimals, DecimalArray, :scale => 5, :precision => 10
|
16
|
+
property :aliases, StringArray, :length => 10
|
16
17
|
end
|
17
|
-
|
18
|
+
|
18
19
|
DataMapper.finalize
|
19
20
|
DataMapper.auto_migrate!
|
20
|
-
|
21
|
+
|
21
22
|
p = Person.new
|
22
23
|
p.info = {:a => "b", :c => "d"}
|
23
24
|
p.decimals = [10.1, 11.2]
|
25
|
+
p.aliases = ["Bob", "George"]
|
24
26
|
p.save
|
25
27
|
@p = Person.last
|
26
28
|
end
|
27
|
-
|
29
|
+
|
28
30
|
it "should provide a person with info" do
|
29
31
|
@p.info.should == {"a" => "b", "c" => "d"}
|
30
32
|
end
|
@@ -33,4 +35,8 @@ describe "dm-pg-types" do
|
|
33
35
|
@p.decimals.should == [10.1,11.2]
|
34
36
|
end
|
35
37
|
|
38
|
+
it "should provide a person with aliases" do
|
39
|
+
@p.aliases.should == ['Bob', 'George']
|
40
|
+
end
|
41
|
+
|
36
42
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
try_spec do
|
4
|
+
|
5
|
+
require './spec/fixtures/person'
|
6
|
+
|
7
|
+
describe DataMapper::TypesFixtures::Person do
|
8
|
+
supported_by :all do
|
9
|
+
before :all do
|
10
|
+
@resource = DataMapper::TypesFixtures::Person.new(:name => '')
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'with no other information' do
|
14
|
+
before :all do
|
15
|
+
@resource.aliases = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'when dumped and loaded again' do
|
19
|
+
before :all do
|
20
|
+
@resource.save.should be(true)
|
21
|
+
@resource.reload
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'has no aliases' do
|
25
|
+
@resource.aliases.should == nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'with no aliases information' do
|
31
|
+
before :all do
|
32
|
+
@resource.aliases = []
|
33
|
+
end
|
34
|
+
|
35
|
+
describe 'when dumped and loaded again' do
|
36
|
+
before :all do
|
37
|
+
@resource.save.should be(true)
|
38
|
+
@resource.reload
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'has empty aliases list' do
|
42
|
+
@resource.aliases.should == []
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'with a few items on the aliases list' do
|
48
|
+
before :all do
|
49
|
+
@input = ['Bob','George','Michael']
|
50
|
+
@resource.aliases = @input
|
51
|
+
end
|
52
|
+
|
53
|
+
describe 'when dumped and loaded again' do
|
54
|
+
before :all do
|
55
|
+
@resource.save.should be(true)
|
56
|
+
@resource.reload
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'includes saved aliases' do
|
60
|
+
@resource.aliases.should == ['Bob','George','Michael']
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
File without changes
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
try_spec do
|
4
|
+
describe DataMapper::Property::StringArray do
|
5
|
+
supported_by :all do
|
6
|
+
before :all do
|
7
|
+
class User
|
8
|
+
include DataMapper::Resource
|
9
|
+
property :id, Serial
|
10
|
+
property :things, StringArray, :length => 10
|
11
|
+
end
|
12
|
+
|
13
|
+
@property = User.properties[:things]
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '.load' do
|
17
|
+
describe "argument is a string that is a string array" do
|
18
|
+
before :all do
|
19
|
+
@input = '{"10.1","2.3","4","5","6"}'
|
20
|
+
@result = @property.load(@input)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "parses the input into an array" do
|
24
|
+
@result.should == ['10.1','2.3','4','5','6']
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".dump" do
|
30
|
+
before :all do
|
31
|
+
@input = ['foo','bar','baz','quux','foo,bar']
|
32
|
+
@result = @property.dump(@input)
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "when argument is an Array" do
|
36
|
+
it "should result in a postgres array string" do
|
37
|
+
@result.should == '{foo,bar,baz,quux,"foo,bar"}'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dm-pg-types
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dm-core
|
@@ -163,7 +163,7 @@ dependencies:
|
|
163
163
|
- - ~>
|
164
164
|
- !ruby/object:Gem::Version
|
165
165
|
version: 1.3.2
|
166
|
-
type: :
|
166
|
+
type: :development
|
167
167
|
prerelease: false
|
168
168
|
version_requirements: !ruby/object:Gem::Requirement
|
169
169
|
none: false
|
@@ -179,7 +179,7 @@ dependencies:
|
|
179
179
|
- - ~>
|
180
180
|
- !ruby/object:Gem::Version
|
181
181
|
version: 1.8.4
|
182
|
-
type: :
|
182
|
+
type: :development
|
183
183
|
prerelease: false
|
184
184
|
version_requirements: !ruby/object:Gem::Requirement
|
185
185
|
none: false
|
@@ -238,17 +238,21 @@ files:
|
|
238
238
|
- lib/dm-pg-types.rb
|
239
239
|
- lib/dm-pg-types/decimal_array.rb
|
240
240
|
- lib/dm-pg-types/hstore.rb
|
241
|
+
- lib/dm-pg-types/pg_array.rb
|
242
|
+
- lib/dm-pg-types/string_array.rb
|
241
243
|
- spec/fixtures/person.rb
|
242
244
|
- spec/integration/decimal_array_spec.rb
|
243
245
|
- spec/integration/full_spec.rb
|
244
246
|
- spec/integration/hstore_spec.rb
|
247
|
+
- spec/integration/string_array_spec.rb
|
245
248
|
- spec/rcov.opts
|
246
249
|
- spec/shared/flags_shared_spec.rb
|
247
250
|
- spec/shared/identity_function_group.rb
|
248
251
|
- spec/spec.opts
|
249
252
|
- spec/spec_helper.rb
|
253
|
+
- spec/unit/decimal_array_spec.rb
|
250
254
|
- spec/unit/hstore_spec.rb
|
251
|
-
- spec/unit/
|
255
|
+
- spec/unit/string_array_spec.rb
|
252
256
|
- tasks/spec.rake
|
253
257
|
- tasks/yard.rake
|
254
258
|
- tasks/yardstick.rake
|
@@ -266,7 +270,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
266
270
|
version: '0'
|
267
271
|
segments:
|
268
272
|
- 0
|
269
|
-
hash: -
|
273
|
+
hash: -3868045948250256032
|
270
274
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
271
275
|
none: false
|
272
276
|
requirements:
|