pg_array 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "minitest", "~> 2.8.0"
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.6.4"
7
+ end
@@ -0,0 +1,18 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ minitest (2.8.0)
10
+ rake (0.9.2.2)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.0.0)
17
+ jeweler (~> 1.6.4)
18
+ minitest (~> 2.8.0)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Carlos Beltrán-Recabarren
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
1
+ = PgArray
2
+
3
+ Convert PostgreSQL arrays to Ruby arrays.
4
+
5
+
6
+ == Example ==
7
+
8
+ require 'pg_array'
9
+
10
+ PgArray.new("{1,2,3}").to_a # => ["1", "2", "3"]
@@ -0,0 +1,37 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'bundler'
4
+
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ require 'rake'
14
+
15
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "pg_array"
19
+ gem.homepage = "http://github.com/cbrecabarren/pg_array"
20
+ gem.license = "MIT"
21
+ gem.summary = "Convert PostgreSQL arrays to Ruby arrays"
22
+ gem.description = "..."
23
+ gem.email = "cbrecabarren@gmail.com"
24
+ gem.authors = ["Carlos Beltrán-Recabarren"]
25
+ gem.files = FileList['lib/**/*', '[A-Z]*'].to_a
26
+ end
27
+
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/*_test.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1 @@
1
+ require 'pg_array/pg_array'
@@ -0,0 +1,115 @@
1
+ # -*- coding: utf-8 -*-
2
+ class PgArray
3
+ def initialize(pg_array_string)
4
+ @pg_array_string = pg_array_string
5
+ end
6
+
7
+ def to_a
8
+ @to_a ||= begin
9
+ if @pg_array_string.nil?
10
+ nil
11
+ else
12
+ parse_array(@pg_array_string)
13
+ end
14
+ end
15
+ end
16
+
17
+
18
+ private
19
+
20
+ # Based on ruby-dbi implementation
21
+ # DBI::DBD::Pg::Type::Array#convert_array
22
+ # http://ruby-dbi.rubyforge.org/
23
+ def parse_array(str)
24
+ array_nesting = 0
25
+ in_string = false
26
+ escaped = false
27
+ sbuffer = ''
28
+ result_array = []
29
+
30
+ str.each_byte do |char|
31
+ char = char.chr
32
+
33
+ if escaped
34
+ sbuffer += char
35
+ escaped = false
36
+ next
37
+ end
38
+
39
+ case char
40
+
41
+
42
+ when '{'
43
+ if in_string
44
+ sbuffer += char
45
+ next
46
+ end
47
+
48
+ if array_nesting >= 1
49
+ sbuffer += char
50
+ end
51
+ array_nesting += 1
52
+
53
+
54
+ when '"'
55
+ sbuffer += char
56
+ in_string = !in_string
57
+
58
+
59
+ when "\\"
60
+ if array_nesting > 1
61
+ sbuffer += char
62
+ else
63
+ escaped = true
64
+ end
65
+
66
+
67
+ when ','
68
+ if in_string or array_nesting > 1
69
+ sbuffer += char
70
+ else
71
+ result_array << new_element(sbuffer)
72
+ sbuffer = ''
73
+ end
74
+
75
+
76
+ when '}'
77
+ if in_string
78
+ sbuffer += char
79
+ next
80
+ end
81
+
82
+ array_nesting -=1
83
+
84
+ if array_nesting == 1
85
+ sbuffer += char
86
+ sbuffer = parse_array(sbuffer)
87
+ elsif array_nesting > 1
88
+ sbuffer += char
89
+ else
90
+ unless sbuffer.nil?
91
+ result_array << new_element(sbuffer)
92
+ end
93
+ end
94
+
95
+
96
+ else
97
+ sbuffer += char
98
+ end
99
+ end
100
+
101
+ result_array
102
+ end
103
+
104
+ def new_element(elem)
105
+ if elem.is_a?(Array)
106
+ elem
107
+ else
108
+ if elem == 'NULL'
109
+ nil
110
+ else
111
+ elem.sub(/^"/, '').sub(/"$/, '')
112
+ end
113
+ end
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_array
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - "Carlos Beltr\xC3\xA1n-Recabarren"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-11-17 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: minitest
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.8.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.6.4
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ description: ...
49
+ email: cbrecabarren@gmail.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - LICENSE
56
+ - README.rdoc
57
+ files:
58
+ - Gemfile
59
+ - Gemfile.lock
60
+ - LICENSE
61
+ - README.rdoc
62
+ - Rakefile
63
+ - VERSION
64
+ - lib/pg_array.rb
65
+ - lib/pg_array/pg_array.rb
66
+ homepage: http://github.com/cbrecabarren/pg_array
67
+ licenses:
68
+ - MIT
69
+ post_install_message:
70
+ rdoc_options: []
71
+
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 919409969
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.8.11
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Convert PostgreSQL arrays to Ruby arrays
96
+ test_files: []
97
+