aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/lib/active_resource/schema_definition.rb
diff options
context:
space:
mode:
authorTaryn East <git@taryneast.org>2009-12-20 18:42:16 -0600
committerJoshua Peek <josh@joshpeek.com>2009-12-20 19:04:53 -0600
commitfc9b3e4a45a81b7526f8154049c825e3755903ad (patch)
tree450129ec1b9f837c993654844e8953ff124daddd /activeresource/lib/active_resource/schema_definition.rb
parent33a6bd390affdeb0b403f513be92a5b1547f6e4e (diff)
downloadrails-fc9b3e4a45a81b7526f8154049c825e3755903ad.tar.gz
rails-fc9b3e4a45a81b7526f8154049c825e3755903ad.tar.bz2
rails-fc9b3e4a45a81b7526f8154049c825e3755903ad.zip
define_schema for Active Resource
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Diffstat (limited to 'activeresource/lib/active_resource/schema_definition.rb')
-rw-r--r--activeresource/lib/active_resource/schema_definition.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/activeresource/lib/active_resource/schema_definition.rb b/activeresource/lib/active_resource/schema_definition.rb
new file mode 100644
index 0000000000..abcbf3ba4e
--- /dev/null
+++ b/activeresource/lib/active_resource/schema_definition.rb
@@ -0,0 +1,58 @@
+require 'active_resource/exceptions'
+
+module ActiveResource # :nodoc:
+ class SchemaDefinition # :nodoc:
+
+ # attributes can be known to be one of these types. They are easy to
+ # cast to/from.
+ KNOWN_ATTRIBUTE_TYPES = %w( string integer float )
+
+ # An array of attribute definitions, representing the attributes that
+ # have been defined.
+ attr_accessor :attrs
+
+ # The internals of an Active Resource SchemaDefinition are very simple -
+ # unlike an Active Record TableDefinition (on which it is based).
+ # It provides a set of convenience methods for people to define their
+ # schema using the syntax:
+ # define_schema do |s|
+ # s.string :foo
+ # s.integer :bar
+ # end
+ #
+ # The schema stores the name and type of each attribute. That is then
+ # read out by the define_schema method to populate the actual
+ # Resource's schema
+ def initialize
+ @attrs = {}
+ end
+
+ def attribute(name, type, options = {})
+ raise ArgumentError, "Unknown Attribute type: #{type.inspect} for key: #{name.inspect}" unless type.nil? || SchemaDefinition::KNOWN_ATTRIBUTE_TYPES.include?(type.to_s)
+
+ the_type = type.to_s
+ # TODO: add defaults
+ #the_attr = [type.to_s]
+ #the_attr << options[:default] if options.has_key? :default
+ @attrs[name.to_s] = the_type
+ self
+ end
+
+ # The following are the attribute types supported by Active Resource
+ # migrations.
+ # TODO: We should eventually support all of these:
+ # %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |attr_type|
+ KNOWN_ATTRIBUTE_TYPES.each do |attr_type|
+ class_eval <<-EOV
+ def #{attr_type.to_s}(*args) # def string(*args)
+ options = args.extract_options! # options = args.extract_options!
+ attr_names = args # attr_names = args
+ #
+ attr_names.each { |name| attribute(name, '#{attr_type}', options) } # attr_names.each { |name| attribute(name, 'string', options) }
+ end # end
+ EOV
+
+ end
+
+ end
+end