Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joshua Peek <josh@joshpeek.com>
- Loading branch information
Showing
3 changed files
with
609 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.