Skip to content

Instantly share code, notes, and snippets.

@deepfryed
Created July 28, 2010 21:24
Show Gist options
  • Save deepfryed/496353 to your computer and use it in GitHub Desktop.
Save deepfryed/496353 to your computer and use it in GitHub Desktop.
diff --git a/examples/scheme.rb b/examples/scheme.rb
index 52e5f98..b06c1e7 100755
--- a/examples/scheme.rb
+++ b/examples/scheme.rb
@@ -5,12 +5,12 @@ require 'pp'
class User < Swift::Scheme
store :users
- attribute :id, Swift::Type::Integer, serial: true, key: true
- attribute :name, Swift::Type::String
- attribute :email, Swift::Type::String
- attribute :active, Swift::Type::Boolean
- attribute :created, Swift::Type::Time, default: proc { Time.now }
- attribute :optional, Swift::Type::String, default: 'woot'
+ attribute :id, :integer, serial: true, key: true
+ attribute :name, :string
+ attribute :email, :string
+ attribute :active, :boolean
+ attribute :created, :time, default: proc { Time.now }
+ attribute :optional, :string, default: 'woot'
end # User
Swift.setup user: Etc.getlogin, db: 'swift', driver: ARGV[0] || 'postgresql'
diff --git a/lib/swift/attribute.rb b/lib/swift/attribute.rb
index f9f72e9..fda61e6 100644
--- a/lib/swift/attribute.rb
+++ b/lib/swift/attribute.rb
@@ -21,5 +21,17 @@ module Swift
def #{name}= value; tuple.store(:#{field}, value) end
RUBY
end
+
+ def self.registry
+ raise NoMethodError, "Cannot call #registry method on #{self}" if self != Swift::Attribute
+ @@registry ||= {}
+ end
+
+ def self.inherited klass
+ if self == Swift::Attribute && klass.name
+ sym = klass.respond_to?(:register_as) ? klass.register_as : klass.name.split(/::/).last.downcase.to_sym
+ registry[sym] = klass
+ end
+ end
end # Attribute
end # Swift
diff --git a/lib/swift/scheme.rb b/lib/swift/scheme.rb
index e4c56fb..ac06919 100644
--- a/lib/swift/scheme.rb
+++ b/lib/swift/scheme.rb
@@ -41,6 +41,8 @@ module Swift
end
def attribute name, type, options = {}
+ type = (type.kind_of?(Class) ? type : Attribute.registry[type.to_sym]) or
+ raise ArgumentError, "Unsupported +type+ #{type}"
attributes.push(attribute = type.new(self, name, options))
(class << self; self end).send(:define_method, name, lambda{ attribute })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment