From 669c5eec445ff097b765c387b92ae1f174134f75 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 20 Dec 2009 18:44:13 -0600 Subject: Rename SchemaDefinition => Schema --- activeresource/lib/active_resource/schema.rb | 58 ++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 activeresource/lib/active_resource/schema.rb (limited to 'activeresource/lib/active_resource/schema.rb') diff --git a/activeresource/lib/active_resource/schema.rb b/activeresource/lib/active_resource/schema.rb new file mode 100644 index 0000000000..498b00ffef --- /dev/null +++ b/activeresource/lib/active_resource/schema.rb @@ -0,0 +1,58 @@ +require 'active_resource/exceptions' + +module ActiveResource # :nodoc: + class Schema # :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 Schema 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? || Schema::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 -- cgit v1.2.3 From c0ad3f6cc618f42eae0c5d5ceefde32ff3342c20 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 20 Dec 2009 18:48:01 -0600 Subject: Rename define_schema => schema --- activeresource/lib/active_resource/schema.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activeresource/lib/active_resource/schema.rb') diff --git a/activeresource/lib/active_resource/schema.rb b/activeresource/lib/active_resource/schema.rb index 498b00ffef..6f0b229145 100644 --- a/activeresource/lib/active_resource/schema.rb +++ b/activeresource/lib/active_resource/schema.rb @@ -15,13 +15,13 @@ module ActiveResource # :nodoc: # 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| + # 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 + # read out by the schema method to populate the actual # Resource's schema def initialize @attrs = {} -- cgit v1.2.3 From 2e9c7759984573944592fb1bda5aeb7c58edba55 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 20 Dec 2009 19:03:22 -0600 Subject: Use instance_eval for schema block --- activeresource/lib/active_resource/schema.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'activeresource/lib/active_resource/schema.rb') diff --git a/activeresource/lib/active_resource/schema.rb b/activeresource/lib/active_resource/schema.rb index 6f0b229145..4ca83e404d 100644 --- a/activeresource/lib/active_resource/schema.rb +++ b/activeresource/lib/active_resource/schema.rb @@ -2,7 +2,6 @@ require 'active_resource/exceptions' module ActiveResource # :nodoc: class Schema # :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 ) @@ -15,9 +14,9 @@ module ActiveResource # :nodoc: # 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: - # schema do |s| - # s.string :foo - # s.integer :bar + # schema do + # string :foo + # integer :bar # end # # The schema stores the name and type of each attribute. That is then @@ -44,15 +43,13 @@ module ActiveResource # :nodoc: # %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) + 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 -- cgit v1.2.3 From bdccffc40eee1e11b7e5f3516bffa4c46bf97b30 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 20 Dec 2009 19:03:47 -0600 Subject: Remove annoying and useless meta comments --- activeresource/lib/active_resource/schema.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'activeresource/lib/active_resource/schema.rb') diff --git a/activeresource/lib/active_resource/schema.rb b/activeresource/lib/active_resource/schema.rb index 4ca83e404d..8368b652c2 100644 --- a/activeresource/lib/active_resource/schema.rb +++ b/activeresource/lib/active_resource/schema.rb @@ -43,12 +43,12 @@ module ActiveResource # :nodoc: # %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 + def #{attr_type.to_s}(*args) + options = args.extract_options! + attr_names = args + + attr_names.each { |name| attribute(name, '#{attr_type}', options) } + end EOV end end -- cgit v1.2.3