dm-postgres-types 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e394e2af566007a38dbea5021a14e2671e44c139
4
+ data.tar.gz: 5d92a8cbf5c4f720ac38acb68db93b5da879b1bb
5
+ SHA512:
6
+ metadata.gz: bf6481a2fd0a8cc750d0e0a30aa06a2ae85b013d119b3971bba51b687ccdd710e729020287740b4ede733fefeac0f87fb6d6c2928d14fa85a6ac33992bb55d71
7
+ data.tar.gz: 31d8dcb892a8090d56b7fa677b7836f8166c2dbe03fa561050cae76031930c63ab4d3fd85b7e0331b285abbc8fb5f8705128854325a36d42172597e8f2bd4887
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Eric Marden
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,120 @@
1
+ # DataMapper::PostgresTypes
2
+
3
+ Adds support for native PostgreSQL datatypes, including JSON, HSTORE, and ARRAY to DataMapper.
4
+
5
+ This gem replaces `dm-pg-types` with a cleaner/faster implementation of the Array and HStore datatypes, and adds support for the PostgreSQL's native JSON datatype (which means it is stored in the database differently than the JSON property offered by `dm-types`.
6
+
7
+ While `dm-postgres-types` is more or less compatible with the other implemenations, I recommend you create new fields and migrate any existing data instead of just changing the property type of existing models and hoping for the best. YMMV, of course.
8
+
9
+ On with the show...
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'dm-postgres-types'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ DataMapper::PostgresTypes defines 4 new `DataMapper::Property` classes that can be used when defining your schema. These properties take advantage of datatypes available in PostgreSQL 8.4+, and in the case of `DataMapper::Property::PgJSON`, PostgreSQL 9.2+. With that said, this gem is recommended for and has only been tested against PostgreSQL 9.3.
24
+
25
+ ### DataMapper::Property::PgArray
26
+
27
+ Example:
28
+
29
+ ````ruby
30
+ class MyModel
31
+ include DataMapper::Resource
32
+
33
+ property :id, Serial
34
+ property :elements, PgArray
35
+ end
36
+
37
+ m = MyModel.new(elements: ['abc', 'def'])
38
+ m.elements
39
+ # => ["abc", "def"]
40
+ ````
41
+
42
+ Use this property when you want to store an array of strings. This property dosn't have any options.
43
+
44
+ ---
45
+
46
+ ### DataMapper::Property::PgNumericArray
47
+
48
+ Example:
49
+
50
+ ````ruby
51
+ class MyModel
52
+ include DataMapper::Resource
53
+
54
+ property :id, Serial
55
+ property :decimals, PgNumericArray, scale: 5, precision: 10
56
+ end
57
+
58
+ m = MyModel.new(decimals: [1.13, 2.19, 5.11])
59
+ m.decimals
60
+ # => [1.13, 2.19, 5.11]
61
+ ````
62
+ Use this property when you want to store an array of integers and/or floats.
63
+
64
+ Precision is the number of digits in your array's value(s). Scale is the number of digits to the right of the decimal point in your array's values. Set these options and PostgreSQL will cooerce your array's value(s) when it stores your array.
65
+
66
+ The default percision is `10` and the default scale is `0`, which is suitable for integers. Adjust the scale if you want to store float values instead.
67
+
68
+ ---
69
+
70
+ ### DataMapper::Property::PgArray
71
+
72
+ Example:
73
+
74
+ ````ruby
75
+ class MyModel
76
+ include DataMapper::Resource
77
+
78
+ property :id, Serial
79
+ property :things, PgHStore
80
+ end
81
+
82
+ m = MyModel.new(things: { a: 'bcd', e: 'fgh' })
83
+ m.things
84
+ # => { "a" => "bcd", "e" => "fgh" }
85
+ ````
86
+
87
+ Use this property when you want to store simple hash values. This property dosn't have any options.
88
+
89
+ Please note: All hash keys will be returned as strings when your hash is loaded from the database, even if you supplied a hash with symbol keys when you saved it. If this is undesired, check out the `Hash#symbolize_keys` method provided by `ActiveSupport` ([link](http://rubygems.org/gems/activesupport)).
90
+
91
+ ---
92
+
93
+ ### DataMapper::Property::PgJSON
94
+
95
+ Example:
96
+
97
+ ````ruby
98
+ class MyModel
99
+ include DataMapper::Resource
100
+
101
+ property :id, Serial
102
+ property :data, PgJSON, load_raw_value: true
103
+ end
104
+
105
+ m = MyModel.new(data: { a: 'bcd', e: 'fgh' })
106
+ m.things
107
+ # => "{"a" => "bcd", "e" => "fgh" }
108
+ ````
109
+
110
+ Use this property when you want to store your value, serialized as JSON. By default, the value will be deserialized back into a ruby datatype (e.g. `Hash` or `Array`) when loaded from the database. Set the `:load_raw_value` to `true`, as shown above, to get the raw JSON value as a string instead.
111
+
112
+ Please note: JSON de/serializtion is being handled by the `Oj` gem, which is the fastest implementation for ruby that you can find. There are no immediate plans to add the overhead of `MultiJSON` to this library. With that said, there shouldn't be any conflicts with other JSON libraries you maybe using in your project.
113
+
114
+ ## Contributing
115
+
116
+ 1. Fork it ( http://github.com/<my-github-username>/dm-postgres-types/fork )
117
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
118
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
119
+ 4. Push to the branch (`git push origin my-new-feature`)
120
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ # environment
6
+ ENV['RACK_ENV'] ||= 'development'
7
+
8
+ # load path
9
+ lib_path = File.expand_path('../lib', __FILE__)
10
+ ($:.unshift lib_path) unless ($:.include? lib_path)
11
+
12
+ Rake::TestTask.new do |t|
13
+ t.libs << "spec"
14
+ t.pattern = "spec/**/*_spec.rb"
15
+ end
16
+ task :default => [:test]
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dm-postgres-types/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "dm-postgres-types"
8
+ gem.version = DataMapper::PostgresTypes::VERSION
9
+ gem.authors = ["Eric Marden"]
10
+ gem.email = ["eric@xentek.net"]
11
+ gem.summary = %q{Adds support for native PostgreSQL datatypes to DataMapper}
12
+ gem.description = %q{Adds support for native PostgreSQL datatypes, including JSON, HSTORE, and Array to DataMapper}
13
+ gem.homepage = ""
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency 'dm-core', '~> 1.2.0'
22
+ gem.add_dependency 'dm-migrations', '~> 1.2.0'
23
+ gem.add_dependency 'dm-types', '~> 1.2.0'
24
+ gem.add_dependency 'dm-validations', '~> 1.2.0'
25
+ gem.add_dependency 'dm-postgres-adapter', '~> 1.2.0'
26
+
27
+ gem.add_development_dependency 'bundler'
28
+ gem.add_development_dependency 'rake'
29
+ gem.add_development_dependency 'gem-release'
30
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+
3
+ # deps
4
+ require 'dm-core'
5
+ require 'dm-migrations'
6
+ require 'dm-types'
7
+ require 'dm-postgres-adapter'
8
+
9
+ # lib
10
+ require 'dm-postgres-types/property/pg_array'
11
+ require 'dm-postgres-types/property/pg_numeric_array'
12
+ require 'dm-postgres-types/property/pg_hstore'
13
+ require 'dm-postgres-types/property/pg_json'
14
+ require 'dm-postgres-types/version'
15
+
16
+ # migrations and primitives
17
+ module DataMapper
18
+ module Migrations
19
+ module PostgresAdapter
20
+ def property_schema_hash(property)
21
+ schema = super
22
+
23
+ if property.kind_of?(Property::DecimalArray)
24
+ schema[:primitive] = "#{schema[:primitive]}(#{property.precision},#{property.scale})[]"
25
+ schema[:precision] = schema[:scale] = nil
26
+ elsif property.kind_of?(Property::StringArray)
27
+ schema[:primitive] = "#{schema[:primitive]}[]"
28
+ schema[:length] = nil
29
+ elsif property.kind_of?(Property::PgJSON)
30
+ schema.delete(:length)
31
+ end
32
+
33
+ schema
34
+ end
35
+ end
36
+ end
37
+
38
+ module PostgresTypes
39
+ def self.included(base)
40
+ base.extend ClassMethods
41
+ end
42
+
43
+ module ClassMethods
44
+ def type_map
45
+ postgres_types = {
46
+ Property::HStore => { primitive: 'HSTORE' },
47
+ Property::DecimalArray => { primitive: "NUMERIC" },
48
+ Property::StringArray => { primitive: "TEXT" },
49
+ Property::PgJSON => { primitive: 'JSON' }
50
+ }
51
+ super.merge(postgres_types).freeze
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ DataMapper::Adapters::PostgresAdapter.send(:include, DataMapper::PostgresTypes)
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ require 'dm-core'
4
+
5
+ module DataMapper
6
+ class Property
7
+ class PgArray < Object
8
+ def load(value)
9
+ return [] if value.nil?
10
+ value.gsub!(/[{}]/,'')
11
+ CSV.parse_line(value) || []
12
+ end
13
+
14
+ def dump(value)
15
+ return nil if value.nil?
16
+ "{#{CSV.generate_line(value, row_sep: '')}}"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ require 'dm-core'
4
+
5
+ module DataMapper
6
+ class Property
7
+ class PgHStore < Object
8
+ def load(value)
9
+ return nil unless value
10
+ values = value.split(",")
11
+ values.map! { |val| unescape_pg_hash(val) }
12
+ values.map! { |key, val| [key, unescape_nil(val)] }
13
+ Hash[*(values.flatten)]
14
+ end
15
+
16
+ def dump(value)
17
+ return "" unless value
18
+ value.map! do |idx, val|
19
+ [escape_double_quote(idx), escape_value(val)].join(",")
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def escape_nil(value)
26
+ (value.nil?) ? 'NULL' : value
27
+ end
28
+
29
+ def unescape_nil(value)
30
+ (value == 'NULL') ? nil : value
31
+ end
32
+
33
+ def escape_double_quote(value)
34
+ value.gsub!(/"/, '\"')
35
+ value
36
+ end
37
+
38
+ def unescape_double_quote(value)
39
+ value.gsub!('"','')
40
+ value.strip!
41
+ value
42
+ end
43
+
44
+ def escape_pg_hash(value)
45
+ (value =~ /[,\s=>]/ || value.empty?) ? %Q{"#{value}"} : value
46
+ end
47
+
48
+ def unescape_pg_hash(value)
49
+ values = value.split("=>")
50
+ values.map! { |val| unescape_double_quote(val) }
51
+ values
52
+ end
53
+
54
+ def escape_value(value)
55
+ escape_double_quote(escape_nil(escape_pg_hash(value)))
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ require 'dm-core'
4
+ require 'dm-types/support/dirty_minder'
5
+
6
+ module DataMapper
7
+ class Property
8
+ class PgJSON < Object
9
+ accept_options :load_raw_value
10
+ attr_reader :load_raw_value
11
+ def initialize(model, name, options = {})
12
+ super
13
+ @load_raw_value = @options[:load_raw_value] || false
14
+ end
15
+
16
+ def dump(value)
17
+ case value
18
+ when ::NilClass, ::String
19
+ value
20
+ when ::Hash, ::Array
21
+ Oj.dump(value, mode: :compat)
22
+ else
23
+ '{}'
24
+ end
25
+ end
26
+
27
+ def load(value)
28
+ case value
29
+ when ::Hash, ::Array
30
+ (load_raw_value) ? Oj.dump(value, mode: :compat) : value
31
+ when ::String
32
+ (load_raw_value) ? value : Oj.load(value)
33
+ else
34
+ (load_raw_value) ? '{}' : {}
35
+ end
36
+ end
37
+
38
+ def primitive?(value)
39
+ value.kind_of?(::String)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ require 'dm-core'
4
+ require 'dm-postgres-types/property/pg_array'
5
+
6
+ module DataMapper
7
+ class Property
8
+ class PgNumericArray < PgArray
9
+ accept_options :precision, :scale
10
+ attr_reader :precision, :scale
11
+
12
+ def initialize(model, name, options = {})
13
+ super
14
+ @precision = @options[:precision] || 10
15
+ @scale = @options[:scale] || 0
16
+
17
+ if precision <= 0
18
+ raise ArgumentError, "precision must be greater than 0"
19
+ end
20
+
21
+ if scale <= 0
22
+ raise ArgumentError, "scale must be greater than or equal to 0"
23
+ end
24
+ end
25
+
26
+ def load(value)
27
+ values = super
28
+ values.map! { |val| (scale > 0) ? val.to_f : val.to_i } if values
29
+ values
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ module DataMapper
2
+ module PostgresTypes
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dm-postgres-types
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eric Marden
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dm-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: dm-migrations
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: dm-types
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: dm-validations
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: dm-postgres-adapter
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.2.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.2.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: gem-release
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Adds support for native PostgreSQL datatypes, including JSON, HSTORE,
126
+ and Array to DataMapper
127
+ email:
128
+ - eric@xentek.net
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - Gemfile
135
+ - LICENSE.txt
136
+ - README.md
137
+ - Rakefile
138
+ - dm-postgres-types.gemspec
139
+ - lib/dm-postgres-types.rb
140
+ - lib/dm-postgres-types/property/pg_array.rb
141
+ - lib/dm-postgres-types/property/pg_hstore.rb
142
+ - lib/dm-postgres-types/property/pg_json.rb
143
+ - lib/dm-postgres-types/property/pg_numeric_array.rb
144
+ - lib/dm-postgres-types/version.rb
145
+ homepage: ''
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.2.0
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Adds support for native PostgreSQL datatypes to DataMapper
169
+ test_files: []