Created
July 21, 2010 01:55
-
-
Save deepfryed/483913 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/examples/orm.rb b/examples/orm.rb | |
index bfb475d..1fb9717 100755 | |
--- a/examples/orm.rb | |
+++ b/examples/orm.rb | |
@@ -1,5 +1,6 @@ | |
#!/usr/bin/env ruby | |
require_relative '../lib/swift-orm' | |
+require_relative '../lib/swift-orm/types' | |
require 'etc' | |
require 'pp' | |
@@ -9,10 +10,11 @@ class User < Swift.resource do | |
property :name, String, index: 'info' | |
property :email, String, index: 'info' | |
property :optional, String, default: 'woot' | |
+ property :category, Swift::Types::Enum["admin", "user"], default: 'user' | |
end | |
end # User | |
-Swift.setup user: Etc.getlogin, driver: 'postgresql', db: 'swift' | |
+Swift.setup user: Etc.getlogin, db: 'swift', driver: ARGV[0] || 'postgresql' | |
Swift.trace true | |
puts '-- auto_migrate! --' | |
diff --git a/lib/swift-orm/migrations.rb b/lib/swift-orm/migrations.rb | |
index 4e66b89..1dbd54e 100644 | |
--- a/lib/swift-orm/migrations.rb | |
+++ b/lib/swift-orm/migrations.rb | |
@@ -9,7 +9,7 @@ module Swift | |
end | |
def migrate_up! resource | |
- fields = resource.properties.map{|p| column_definition(p)}.join(', ') | |
+ fields = resource.properties.map{|p| column_definition(p, resource)}.join(', ') | |
keys = resource.properties.keys | |
fields += ', primary key (%s)' % keys.join(', ') unless keys.empty? or resource.properties.serial? | |
@adapter.execute "create table #{resource.store} (#{fields})" | |
@@ -19,8 +19,15 @@ module Swift | |
end | |
end | |
- def column_definition property | |
- "#{property.field} #{property.serial ? typemap[:serial] : typemap[property.type] || 'text'}" | |
+ def column_definition property, resource | |
+ type, *commands = if property.type.respond_to?(:storage_type) | |
+ property.type.storage_type(property, resource, @adapter.driver) | |
+ else | |
+ "#{property.serial ? typemap[:serial] : typemap[property.type] || 'text'}" | |
+ end | |
+ | |
+ commands.each {|command| @adapter.execute(command)} unless commands.empty? | |
+ "#{property.field} #{type}" | |
end | |
def migrate_down! resource | |
diff --git a/lib/swift-orm/types.rb b/lib/swift-orm/types.rb | |
new file mode 100644 | |
index 0000000..45ba416 | |
--- /dev/null | |
+++ b/lib/swift-orm/types.rb | |
@@ -0,0 +1,31 @@ | |
+module Swift | |
+ module Types | |
+ class Enum | |
+ def initialize value | |
+ if !self.class.values.include?(value) | |
+ raise ArgumentError, "#{value} not found in (%s)" % self.class.values.join(',') | |
+ end | |
+ @value = value | |
+ end | |
+ | |
+ class << self | |
+ attr_reader :values | |
+ def [] *values | |
+ Class.new(self) do | |
+ @values = values.flatten | |
+ end | |
+ end | |
+ def storage_type property, resource, driver | |
+ defn = 'enum(%s)' % values.map{|v| "'#{v}'"}.join(',') | |
+ case driver.to_s | |
+ when 'postgresql' | |
+ name = "#{resource.store}_#{property.field}_type" | |
+ [ name, "drop type if exists #{name}", "create type #{name} as #{defn}" ] | |
+ else | |
+ defn | |
+ end | |
+ end | |
+ end | |
+ end # Enum | |
+ end # Types | |
+end # Swift |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment