pg_enum 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +33 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/lib/pg_enum/active_record.rb +189 -0
- data/lib/pg_enum.rb +1 -0
- metadata +70 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 BradfordW
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= pg_enum
|
2
|
+
|
3
|
+
Adding enum type support to ActiveRecord's PostgreSQL adapter.
|
4
|
+
|
5
|
+
I recommend validating your input via validates_inclusion_of.
|
6
|
+
|
7
|
+
For example:
|
8
|
+
|
9
|
+
class Sport < ActiveRecord::Base
|
10
|
+
validates_presence_of :type, :in => ['hockey','football','soccer']
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
And you can also use your types in migrations like so:
|
15
|
+
def self.up
|
16
|
+
create_enum :sports_type, :values => ['hockey','football','soccer]
|
17
|
+
create_table :sports do |t|
|
18
|
+
t.sports_type :type
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.down
|
23
|
+
drop_table :sports #have to drop all dependencies first
|
24
|
+
drop_enum :sports_type
|
25
|
+
end
|
26
|
+
|
27
|
+
== Note on Patches/Pull Requests
|
28
|
+
|
29
|
+
If you fix stuff, I'll probably take it.
|
30
|
+
|
31
|
+
|
32
|
+
== Copyright
|
33
|
+
Just give credit where it's due.
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "pg_enum"
|
8
|
+
gem.summary = "Adds native enum support to the Rails3 ActiveRecord PostgreSQL adapter"
|
9
|
+
gem.description = gem.summary
|
10
|
+
gem.homepage = "http://github.com/bradfordw/pg_enum"
|
11
|
+
gem.authors = ["BradfordW"]
|
12
|
+
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/test_*.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/test_*.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
task :test => :check_dependencies
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "pg_enum #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,189 @@
|
|
1
|
+
module PgEnum
|
2
|
+
module ActiveRecord
|
3
|
+
module ConnectionAdapters
|
4
|
+
|
5
|
+
module SchemaStatements
|
6
|
+
def create_enum(enum_name, options = {})
|
7
|
+
if options[:force] && enum_exists?(enum_name)
|
8
|
+
drop_enum(enum_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
create_enum_sql = "CREATE TYPE #{enum_name} AS ENUM ('"
|
12
|
+
create_enum_sql << options[:values].join("','")
|
13
|
+
create_enum_sql << "')"
|
14
|
+
execute create_enum_sql
|
15
|
+
end
|
16
|
+
|
17
|
+
def drop_enum(enum_name)
|
18
|
+
execute "drop type #{enum_name}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def enum_exists?(enum_name)
|
22
|
+
res = execute("select count(1) from pg_catalog.pg_type where typcategory = 'E' and typname = '#{enum_name}'")
|
23
|
+
(res.try(:first).try(:values).try(:first).to_i > 0)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Column
|
28
|
+
def klass
|
29
|
+
case type
|
30
|
+
when :integer then Fixnum
|
31
|
+
when :float then Float
|
32
|
+
when :decimal then BigDecimal
|
33
|
+
when :datetime then Time
|
34
|
+
when :date then Date
|
35
|
+
when :timestamp then Time
|
36
|
+
when :time then Time
|
37
|
+
when :text, :string then String
|
38
|
+
when :binary then String
|
39
|
+
when :boolean then Object
|
40
|
+
else String
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class TableDefinition
|
46
|
+
def method_missing(symbol, *args)
|
47
|
+
if symbol.to_s == 'xml'
|
48
|
+
xml_column_fallback(args)
|
49
|
+
else
|
50
|
+
if PostgreSQLAdapter::NATIVE_DATABASE_TYPES.has_key?(symbol)
|
51
|
+
enum_column_fallback(symbol, args)
|
52
|
+
else
|
53
|
+
super
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def enum_column_fallback(type, *args)
|
59
|
+
case @base.adapter_name.downcase
|
60
|
+
when 'postgresql'
|
61
|
+
options = args.extract_options!
|
62
|
+
column(args[0], type, options)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class PostgreSQLAdapter < AbstractAdapter
|
68
|
+
NATIVE_DATABASE_TYPES[:counting] = {:name => 'counting'}
|
69
|
+
|
70
|
+
def type_to_sql(type, limit = nil, precision = nil, scale = nil)
|
71
|
+
return 'string' if type.to_s == 'enumerable2'
|
72
|
+
return super unless type.to_s == 'integer'
|
73
|
+
return 'integer' unless limit
|
74
|
+
|
75
|
+
case limit
|
76
|
+
when 1, 2; 'smallint'
|
77
|
+
when 3, 4; 'integer'
|
78
|
+
when 5..8; 'bigint'
|
79
|
+
else raise(ActiveRecordError, "No integer type has byte size #{limit}. Use a numeric with precision 0 instead.")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
class PostgreSQLColumn < Column
|
86
|
+
private
|
87
|
+
def simplified_type(field_type)
|
88
|
+
case field_type
|
89
|
+
when 'counting'
|
90
|
+
:string
|
91
|
+
# Numeric and monetary types
|
92
|
+
when /^(?:real|double precision)$/
|
93
|
+
:float
|
94
|
+
# Monetary types
|
95
|
+
when 'money'
|
96
|
+
:decimal
|
97
|
+
# Character types
|
98
|
+
when /^(?:character varying|bpchar)(?:\(\d+\))?$/
|
99
|
+
:string
|
100
|
+
# Binary data types
|
101
|
+
when 'bytea'
|
102
|
+
:binary
|
103
|
+
# Date/time types
|
104
|
+
when /^timestamp with(?:out)? time zone$/
|
105
|
+
:datetime
|
106
|
+
when 'interval'
|
107
|
+
:string
|
108
|
+
# Geometric types
|
109
|
+
when /^(?:point|line|lseg|box|"?path"?|polygon|circle)$/
|
110
|
+
:string
|
111
|
+
# Network address types
|
112
|
+
when /^(?:cidr|inet|macaddr)$/
|
113
|
+
:string
|
114
|
+
# Bit strings
|
115
|
+
when /^bit(?: varying)?(?:\(\d+\))?$/
|
116
|
+
:string
|
117
|
+
# XML type
|
118
|
+
when 'xml'
|
119
|
+
:xml
|
120
|
+
# Arrays
|
121
|
+
when /^\D+\[\]$/
|
122
|
+
:string
|
123
|
+
# Object identifier types
|
124
|
+
when 'oid'
|
125
|
+
:integer
|
126
|
+
# UUID type
|
127
|
+
when 'uuid'
|
128
|
+
:string
|
129
|
+
# Small and big integer types
|
130
|
+
when /^(?:small|big)int$/
|
131
|
+
:integer
|
132
|
+
# Pass through all types that are not specific to PostgreSQL.
|
133
|
+
else
|
134
|
+
super
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def self.extract_value_from_default(default)
|
139
|
+
case default
|
140
|
+
# Numeric types
|
141
|
+
when /\A\(?(-?\d+(\.\d*)?\)?)\z/
|
142
|
+
$1
|
143
|
+
# Character types
|
144
|
+
when /\A'(.*)'::(?:character varying|bpchar|text)\z/m
|
145
|
+
$1
|
146
|
+
# Character types (8.1 formatting)
|
147
|
+
when /\AE'(.*)'::(?:character varying|bpchar|text)\z/m
|
148
|
+
$1.gsub(/\\(\d\d\d)/) { $1.oct.chr }
|
149
|
+
# Binary data types
|
150
|
+
when /\A'(.*)'::bytea\z/m
|
151
|
+
$1
|
152
|
+
# Date/time types
|
153
|
+
when /\A'(.+)'::(?:time(?:stamp)? with(?:out)? time zone|date)\z/
|
154
|
+
$1
|
155
|
+
when /\A'(.*)'::interval\z/
|
156
|
+
$1
|
157
|
+
# Boolean type
|
158
|
+
when 'true'
|
159
|
+
true
|
160
|
+
when 'false'
|
161
|
+
false
|
162
|
+
# Geometric types
|
163
|
+
when /\A'(.*)'::(?:point|line|lseg|box|"?path"?|polygon|circle)\z/
|
164
|
+
$1
|
165
|
+
# Network address types
|
166
|
+
when /\A'(.*)'::(?:cidr|inet|macaddr)\z/
|
167
|
+
$1
|
168
|
+
# Bit string types
|
169
|
+
when /\AB'(.*)'::"?bit(?: varying)?"?\z/
|
170
|
+
$1
|
171
|
+
# XML type
|
172
|
+
when /\A'(.*)'::xml\z/m
|
173
|
+
$1
|
174
|
+
# Arrays
|
175
|
+
when /\A'(.*)'::"?\D+"?\[\]\z/
|
176
|
+
$1
|
177
|
+
# Object identifier types
|
178
|
+
when /\A-?\d+\z/
|
179
|
+
$1
|
180
|
+
else
|
181
|
+
# Anything else is blank, some user type, or some function
|
182
|
+
# and we can't know the value of that, so return nil.
|
183
|
+
nil
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
data/lib/pg_enum.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'pg_enum/active_record'
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- BradfordW
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-22 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Adds native enum support to the Rails3 ActiveRecord PostgreSQL adapter
|
22
|
+
email:
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- lib/pg_enum.rb
|
38
|
+
- lib/pg_enum/active_record.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/bradfordw/pg_enum
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --charset=UTF-8
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.6
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Adds native enum support to the Rails3 ActiveRecord PostgreSQL adapter
|
69
|
+
test_files: []
|
70
|
+
|