diff options
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 18 | ||||
-rw-r--r-- | activeresource/lib/active_resource/schema.rb | 11 | ||||
-rw-r--r-- | activeresource/test/cases/base/schema_test.rb | 102 |
3 files changed, 69 insertions, 62 deletions
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index d07571e1d7..b833e9c8ce 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -254,18 +254,18 @@ module ActiveResource # # example: # class Person < ActiveResource::Base - # schema do |s| + # schema do # # define each attribute separately - # s.attribute 'name', :string + # attribute 'name', :string # # # or use the convenience methods and pass >=1 attribute names - # s.string 'eye_colour', 'hair_colour' - # s.integer 'age' - # s.float 'height', 'weight' + # string 'eye_colour', 'hair_colour' + # integer 'age' + # float 'height', 'weight' # # # unsupported types should be left as strings # # overload the accessor methods if you need to convert them - # s.attribute 'created_at', 'string' + # attribute 'created_at', 'string' # end # end # @@ -297,7 +297,7 @@ module ActiveResource def schema(&block) if block_given? schema_definition = Schema.new - yield schema_definition + schema_definition.instance_eval(&block) # skip out if we didn't define anything return unless schema_definition.attrs.present? @@ -343,8 +343,8 @@ module ActiveResource raise ArgumentError, "Expected a hash" unless the_schema.kind_of? Hash - schema do |s| - the_schema.each {|k,v| s.attribute(k,v) } + schema do + the_schema.each {|k,v| attribute(k,v) } end end 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 diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb index 98a8b98da1..d1afb9f439 100644 --- a/activeresource/test/cases/base/schema_test.rb +++ b/activeresource/test/cases/base/schema_test.rb @@ -187,50 +187,57 @@ class SchemaTest < ActiveModel::TestCase assert Person.respond_to?(:schema), "should at least respond to the schema method" assert_nothing_raised("Should allow the schema to take a block") do - Person.schema do |s| - assert s.kind_of?(ActiveResource::Schema), "the 's' should be a schema definition or we're way off track..." - end + Person.schema { } end end test "schema definition should store and return attribute set" do assert_nothing_raised do - Person.schema do |s| - assert s.respond_to?(:attrs), "should return attributes in theory" - s.attribute :foo, :string - assert_equal({'foo' => 'string' }, s.attrs, "should return attributes in practice") + s = nil + Person.schema do + s = self + attribute :foo, :string end + assert s.respond_to?(:attrs), "should return attributes in theory" + assert_equal({'foo' => 'string' }, s.attrs, "should return attributes in practice") end end test "should be able to add attributes through schema" do assert_nothing_raised do - Person.schema do |s| - assert s.attribute('foo', 'string'), "should take a simple attribute" - assert s.attrs.has_key?('foo'), "should have saved the attribute name" - assert_equal 'string', s.attrs['foo'], "should have saved the attribute type" + s = nil + Person.schema do + s = self + attribute('foo', 'string') end + assert s.attrs.has_key?('foo'), "should have saved the attribute name" + assert_equal 'string', s.attrs['foo'], "should have saved the attribute type" end end test "should convert symbol attributes to strings" do assert_nothing_raised do - Person.schema do |s| - assert s.attribute(:foo, :integer), "should take a simple attribute as symbols" - assert s.attrs.has_key?('foo'), "should have saved the attribute name as a string" - assert_equal 'integer', s.attrs['foo'], "should have saved the attribute type as a string" + s = nil + Person.schema do + attribute(:foo, :integer) + s = self end + + assert s.attrs.has_key?('foo'), "should have saved the attribute name as a string" + assert_equal 'integer', s.attrs['foo'], "should have saved the attribute type as a string" end end test "should be able to add all known attribute types" do - Person.schema do |s| + assert_nothing_raised do ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type| - assert_nothing_raised do - assert s.attribute('foo', the_type), "should take a simple attribute of type: #{the_type}" - assert s.attrs.has_key?('foo'), "should have saved the attribute name" - assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}" + s = nil + Person.schema do + s = self + attribute('foo', the_type) end + assert s.attrs.has_key?('foo'), "should have saved the attribute name" + assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}" end end end @@ -238,14 +245,18 @@ class SchemaTest < ActiveModel::TestCase test "attributes should not accept unknown values" do bad_values = [ :oogle, :blob, 'thing'] - Person.schema do |s| - bad_values.each do |bad_value| - assert_raises(ArgumentError,"should only accept a known attribute type, but accepted: #{bad_value.inspect}") do - s.attribute 'key', bad_value + bad_values.each do |bad_value| + s = nil + assert_raises(ArgumentError,"should only accept a known attribute type, but accepted: #{bad_value.inspect}") do + Person.schema do + s = self + attribute 'key', bad_value end - assert !s.respond_to?(bad_value), "should only respond to a known attribute type, but accepted: #{bad_value.inspect}" - assert_raises(NoMethodError,"should only have methods for known attribute types, but accepted: #{bad_value.inspect}") do - s.send bad_value, 'key' + end + assert !self.respond_to?(bad_value), "should only respond to a known attribute type, but accepted: #{bad_value.inspect}" + assert_raises(NoMethodError,"should only have methods for known attribute types, but accepted: #{bad_value.inspect}") do + Person.schema do + send bad_value, 'key' end end end @@ -253,28 +264,27 @@ class SchemaTest < ActiveModel::TestCase test "should accept attribute types as the type's name as the method" do - Person.schema do |s| - ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type| - assert s.respond_to?(the_type), "should recognise the attribute-type: #{the_type} as a method" - assert_nothing_raised("should take the method #{the_type} with the attribute name") do - s.send(the_type,'foo') # eg s.string :foo - end - assert s.attrs.has_key?('foo'), "should now have saved the attribute name" - assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}" + ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type| + s = nil + Person.schema do + s = self + send(the_type,'foo') end + assert s.attrs.has_key?('foo'), "should now have saved the attribute name" + assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}" end end test "should accept multiple attribute names for an attribute method" do names = ['foo','bar','baz'] - Person.schema do |s| - assert_nothing_raised("should take strings with multiple attribute names as params") do - s.string( *names) - end - names.each do |the_name| - assert s.attrs.has_key?(the_name), "should now have saved the attribute name: #{the_name}" - assert_equal 'string', s.attrs[the_name], "should have saved the attribute as a string" - end + s = nil + Person.schema do + s = self + string(*names) + end + names.each do |the_name| + assert s.attrs.has_key?(the_name), "should now have saved the attribute name: #{the_name}" + assert_equal 'string', s.attrs[the_name], "should have saved the attribute as a string" end end @@ -294,7 +304,7 @@ class SchemaTest < ActiveModel::TestCase assert_nothing_raised do Person.schema = {new_attr_name.to_s => 'string'} - Person.schema {|s| s.string new_attr_name_two } + Person.schema { string new_attr_name_two } end assert Person.new.respond_to?(new_attr_name), "should respond to the attribute in a passed-in schema, but failed on: #{new_attr_name}" @@ -311,7 +321,7 @@ class SchemaTest < ActiveModel::TestCase assert !Person.new.respond_do?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" assert_nothing_raised do - Person.schema {|s| s.string new_attr_name_two } + Person.schema { string new_attr_name_two } Person.schema = {new_attr_name.to_s => 'string'} end @@ -335,7 +345,7 @@ class SchemaTest < ActiveModel::TestCase end Person.schema = {new_attr_name.to_s => :float} - Person.schema {|s| s.string new_attr_name_two } + Person.schema { string new_attr_name_two } assert_nothing_raised do Person.new.send(new_attr_name) |