From fc9b3e4a45a81b7526f8154049c825e3755903ad Mon Sep 17 00:00:00 2001 From: Taryn East Date: Sun, 20 Dec 2009 18:42:16 -0600 Subject: define_schema for Active Resource Signed-off-by: Joshua Peek --- activeresource/lib/active_resource/base.rb | 144 +++++++- .../lib/active_resource/schema_definition.rb | 58 +++ activeresource/test/cases/base/schema_test.rb | 409 +++++++++++++++++++++ 3 files changed, 609 insertions(+), 2 deletions(-) create mode 100644 activeresource/lib/active_resource/schema_definition.rb create mode 100644 activeresource/test/cases/base/schema_test.rb (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 18105e8887..60bd573911 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -13,6 +13,7 @@ require 'set' require 'uri' require 'active_resource/exceptions' +require 'active_resource/schema_definition' module ActiveResource # ActiveResource::Base is the main class for mapping RESTful resources as models in a Rails application. @@ -241,6 +242,127 @@ module ActiveResource cattr_accessor :logger class << self + # This will shortly disappear to be replaced by the migration-style + # usage of this for defining a schema. At that point, all the doc + # currenlty on schema= will move back here... + def schema # :nodoc: + @schema ||= nil + end + # Creates a schema for this resource - setting the attributes that are + # known prior to fetching an instance from the remote system. + # + # The schema helps define the set of known_attributes of the + # current resource. + # + # There is no need to specify a schema for your Active Resource. If + # you do not, the known_attributes will be guessed from the + # instance attributes returned when an instance is fetched from the + # remote system. + # + # example: + # class Person < ActiveResource::Base + # define_schema do |s| + # # define each attribute separately + # s.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' + # + # # unsupported types should be left as strings + # # overload the accessor methods if you need to convert them + # s.attribute 'created_at', 'string' + # end + # end + # + # p = Person.new + # p.respond_to? :name # => true + # p.respond_to? :age # => true + # p.name # => nil + # p.age # => nil + # + # j = Person.find_by_name('John') # John343 + # j.respond_to? :name # => true + # j.respond_to? :age # => true + # j.name # => 'John' + # j.age # => '34' # note this is a string! + # j.num_children # => '3' # note this is a string! + # + # p.num_children # => NoMethodError + # + # Attribute-types must be one of: + # string, integer, float + # + # Note: at present the attribute-type doesn't do anything, but stay + # tuned... + # Shortly it will also *cast* the value of the returned attribute. + # ie: + # j.age # => 34 # cast to an integer + # j.weight # => '65' # still a string! + # + def define_schema + schema_definition = SchemaDefinition.new + yield schema_definition if block_given? + + # skip out if we didn't define anything + return unless schema_definition.attrs.present? + + @schema ||= {}.with_indifferent_access + @known_attributes ||= [] + + schema_definition.attrs.each do |k,v| + @schema[k] = v + @known_attributes << k + end + + schema + end + + + # Alternative, direct way to specify a schema for this + # Resource. define_schema is more flexible, but this is quick + # for a very simple schema. + # + # Pass the schema as a hash with the keys being the attribute-names + # and the value being one of the accepted attribute types (as defined + # in define_schema) + # + # example: + # + # class Person < ActiveResource::Base + # schema = {'name' => :string, 'age' => :integer } + # end + # + # The keys/values can be strings or symbols. They will be converted to + # strings. + # + def schema=(the_schema) + unless the_schema.present? + # purposefully nulling out the schema + @schema = nil + @known_attributes = [] + return + end + + raise ArgumentError, "Expected a hash" unless the_schema.kind_of? Hash + + define_schema do |s| + the_schema.each {|k,v| s.attribute(k,v) } + end + end + + # Returns the list of known attributes for this resource, gathered + # from the provided schema + # Attributes that are known will cause your resource to return 'true' + # when respond_to? is called on them. A known attribute will + # return nil if not set (rather than MethodNotFound); thus + # known attributes can be used with validates_presence_of + # without a getter-method. + def known_attributes + @known_attributes ||= [] + end + # Gets the URI of the REST resources to map for this class. The site variable is required for # Active Resource's mapping to work. def site @@ -776,6 +898,21 @@ module ActiveResource attr_accessor :attributes #:nodoc: attr_accessor :prefix_options #:nodoc: + # If no schema has been defined for the class (see + # ActiveResource::schema=), the default automatic schema is + # generated from the current instance's attributes + def schema + self.class.schema || self.attributes + end + + # This is a list of known attributes for this resource. Either + # gathered fromthe provided schema, or from the attributes + # set on this instance after it has been fetched from the remote system. + def known_attributes + self.class.known_attributes + self.attributes.keys.map(&:to_s) + end + + # Constructor method for \new resources; the optional +attributes+ parameter takes a \hash # of attributes for the \new resource. # @@ -1157,7 +1294,7 @@ module ActiveResource method_name = method.to_s if attributes.nil? super - elsif attributes.has_key?(method_name) + elsif known_attributes.include?(method_name) true elsif method_name =~ /(?:=|\?)$/ && attributes.include?($`) true @@ -1262,7 +1399,10 @@ module ActiveResource attributes[$`] end else - attributes.include?(method_name) ? attributes[method_name] : super + return attributes[method_name] if attributes.include?(method_name) + # not set right now but we know about it + return nil if known_attributes.include?(method_name) + super end end end 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 diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb new file mode 100644 index 0000000000..398e7cf539 --- /dev/null +++ b/activeresource/test/cases/base/schema_test.rb @@ -0,0 +1,409 @@ +require 'abstract_unit' +require "fixtures/person" +require "fixtures/street_address" + +######################################################################## +# Testing the schema of your Active Resource models +######################################################################## +class SchemaTest < ActiveModel::TestCase + def setup + @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') + @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') + @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') + @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address') + @default_request_headers = { 'Content-Type' => 'application/xml' } + @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") + @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people') + @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people') + @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses') + + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/people/1.xml", {}, @matz + mock.get "/people/2.xml", {}, @david + mock.get "/people/Greg.xml", {}, @greg + mock.get "/people/4.xml", {'key' => 'value'}, nil, 404 + mock.get "/people/5.xml", {}, @rick + mock.put "/people/1.xml", {}, nil, 204 + mock.delete "/people/1.xml", {}, nil, 200 + mock.delete "/people/2.xml", {}, nil, 400 + mock.get "/people/99.xml", {}, nil, 404 + mock.post "/people.xml", {}, @rick, 201, 'Location' => '/people/5.xml' + mock.get "/people.xml", {}, @people + mock.get "/people/1/addresses.xml", {}, @addresses + mock.get "/people/1/addresses/1.xml", {}, @addy + mock.get "/people/1/addresses/2.xml", {}, nil, 404 + mock.get "/people/2/addresses/1.xml", {}, nil, 404 + mock.get "/people/Greg/addresses/1.xml", {}, @addy + mock.put "/people/1/addresses/1.xml", {}, nil, 204 + mock.delete "/people/1/addresses/1.xml", {}, nil, 200 + mock.post "/people/1/addresses.xml", {}, nil, 201, 'Location' => '/people/1/addresses/5' + mock.get "/people//addresses.xml", {}, nil, 404 + mock.get "/people//addresses/1.xml", {}, nil, 404 + mock.put "/people//addressaddresseses/1.xml", {}, nil, 404 + mock.delete "/people//addresses/1.xml", {}, nil, 404 + mock.post "/people//addresses.xml", {}, nil, 404 + mock.head "/people/1.xml", {}, nil, 200 + mock.head "/people/Greg.xml", {}, nil, 200 + mock.head "/people/99.xml", {}, nil, 404 + mock.head "/people/1/addresses/1.xml", {}, nil, 200 + mock.head "/people/1/addresses/2.xml", {}, nil, 404 + mock.head "/people/2/addresses/1.xml", {}, nil, 404 + mock.head "/people/Greg/addresses/1.xml", {}, nil, 200 + end + + Person.user = nil + Person.password = nil + end + def teardown + Person.schema = nil # hack to stop test bleedthrough... + end + + + ##################################################### + # Passing in a schema directly and returning it + #### + + test "schema on a new model should be empty" do + assert Person.schema.blank?, "should have a blank class schema" + assert Person.new.schema.blank?, "should have a blank instance schema" + end + + test "schema should only accept a hash" do + ["blahblah", ['one','two'], [:age, :name], Person.new].each do |bad_schema| + assert_raises(ArgumentError,"should only accept a hash (or nil), but accepted: #{bad_schema.inspect}") do + Person.schema = bad_schema + end + end + end + + test "schema should accept a simple hash" do + new_schema = {'age' => 'integer', 'name' => 'string'} + + assert_nothing_raised { Person.schema = new_schema } + assert_equal new_schema, Person.schema + end + + test "schema should accept a hash with simple values" do + new_schema = {'age' => 'integer', 'name' => 'string', 'height' => 'float', 'mydatetime' => 'string'} + + assert_nothing_raised { Person.schema = new_schema } + assert_equal new_schema, Person.schema + end + + test "schema should accept all known attribute types as values" do + # I'd prefer to use here... + ActiveResource::SchemaDefinition::KNOWN_ATTRIBUTE_TYPES.each do |the_type| + assert_nothing_raised("should have accepted #{the_type.inspect}"){ Person.schema = {'my_key' => the_type }} + end + end + + test "schema should not accept unknown values" do + bad_values = [ :oogle, :blob, 'thing'] + + bad_values.each do |bad_value| + assert_raises(ArgumentError,"should only accept a known attribute type, but accepted: #{bad_value.inspect}") do + Person.schema = {'key' => bad_value} + end + end + end + + test "schema should accept nil and remove the schema" do + new_schema = {'age' => 'integer', 'name' => 'string'} + assert_nothing_raised { Person.schema = new_schema } + assert_equal new_schema, Person.schema # sanity check + + + assert_nothing_raised { Person.schema = nil } + assert_nil Person.schema, "should have nulled out the schema, but still had: #{Person.schema.inspect}" + end + + + test "schema should be with indifferent access" do + new_schema = {:age => :integer, 'name' => 'string'} + new_schema_syms = new_schema.keys + + assert_nothing_raised { Person.schema = new_schema } + new_schema_syms.each do |col| + assert Person.new.respond_to?(col.to_s), "should respond to the schema's string key, but failed on: #{col.to_s}" + assert Person.new.respond_to?(col.to_sym), "should respond to the schema's symbol key, but failed on: #{col.to_sym}" + end + end + + + test "schema on a fetched resource should return all the attributes of that model instance" do + p = Person.find(1) + s = p.schema + + assert s.present?, "should have found a non-empty schema!" + + p.attributes.each do |the_attr, val| + assert s.has_key?(the_attr), "should have found attr: #{the_attr} in schema, but only had: #{s.inspect}" + end + end + + test "with two instances, default schema should match the attributes of the individual instances - even if they differ" do + matz = Person.find(1) + rick = Person.find(5) + + m_attrs = matz.attributes.keys.sort + r_attrs = rick.attributes.keys.sort + + assert_not_equal m_attrs, r_attrs, "should have different attributes on each model" + + assert_not_equal matz.schema, rick.schema, "should have had different schemas too" + end + + test "defining a schema should return it when asked" do + assert Person.schema.blank?, "should have a blank class schema" + new_schema = {'name' => 'string', 'age' => 'integer', 'height' => 'float', 'weight' => 'float'} + assert_nothing_raised { + Person.schema = new_schema + assert_equal new_schema, Person.schema, "should have saved the schema on the class" + assert_equal new_schema, Person.new.schema, "should have mde the schema available to every instance" + } + end + + test "defining a schema, then fetching a model should still match the defined schema" do + # sanity checks + assert Person.schema.blank?, "should have a blank class schema" + new_schema = {'name' => 'string', 'age' => 'integer', 'height' => 'float', 'weight' => 'float'} + + matz = Person.find(1) + assert !matz.schema.blank?, "should have some sort of schema on an instance variable" + assert_not_equal new_schema, matz.schema, "should not have the class-level schema until it's been added to the class!" + + assert_nothing_raised { + Person.schema = new_schema + assert_equal new_schema, matz.schema, "class-level schema should override instance-level schema" + } + end + + + ##################################################### + # Using the define_schema syntax + #### + + test "should be able to use define_schema" do + assert Person.respond_to?(:define_schema), "should at least respond to the define_schema method" + + assert_nothing_raised("Should allow the define_schema to take a block") do + Person.define_schema do |s| + assert s.kind_of?(ActiveResource::SchemaDefinition), "the 's' should be a schema definition or we're way off track..." + end + end + end + + test "schema definition should store and return attribute set" do + assert_nothing_raised do + Person.define_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") + end + end + end + + test "should be able to add attributes through define_schema" do + assert_nothing_raised do + Person.define_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" + end + end + end + + test "should convert symbol attributes to strings" do + assert_nothing_raised do + Person.define_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" + end + end + end + + test "should be able to add all known attribute types" do + Person.define_schema do |s| + ActiveResource::SchemaDefinition::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}" + end + end + end + end + + test "attributes should not accept unknown values" do + bad_values = [ :oogle, :blob, 'thing'] + + Person.define_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 + 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 + end + end + end + + + test "should accept attribute types as the type's name as the method" do + Person.define_schema do |s| + ActiveResource::SchemaDefinition::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}" + end + end + end + + test "should accept multiple attribute names for an attribute method" do + names = ['foo','bar','baz'] + Person.define_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 + end + end + + ##################################################### + # What a schema does for us + #### + + # respond_to? + + test "should respond positively to attributes that are only in the schema" do + new_attr_name = :my_new_schema_attribute + new_attr_name_two = :another_new_schema_attribute + assert Person.schema.blank?, "sanity check - should have a blank class schema" + + assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" + 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 = {new_attr_name.to_s => 'string'} + Person.define_schema {|s| s.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}" + assert Person.new.respond_to?(new_attr_name_two), "should respond to the attribute from the define_schema, but failed on: #{new_attr_name_two}" + end + + test "should not care about ordering of schema definitions" do + new_attr_name = :my_new_schema_attribute + new_attr_name_two = :another_new_schema_attribute + + assert Person.schema.blank?, "sanity check - should have a blank class schema" + + assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" + 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.define_schema {|s| s.string new_attr_name_two } + Person.schema = {new_attr_name.to_s => 'string'} + 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}" + assert Person.new.respond_to?(new_attr_name_two), "should respond to the attribute from the define_schema, but failed on: #{new_attr_name_two}" + end + + # method_missing effects + + test "should not give method_missing for attribute only in schema" do + new_attr_name = :another_new_schema_attribute + new_attr_name_two = :another_new_schema_attribute + + assert Person.schema.blank?, "sanity check - should have a blank class schema" + + assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name} as a method") do + Person.new.send(new_attr_name) + end + assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name_two} as a method") do + Person.new.send(new_attr_name_two) + end + + Person.schema = {new_attr_name.to_s => :float} + Person.define_schema {|s| s.string new_attr_name_two } + + assert_nothing_raised do + Person.new.send(new_attr_name) + Person.new.send(new_attr_name_two) + end + end + + + ######## + # Known attributes + # + # Attributes can be known to be attributes even if they aren't actually + # 'set' on a particular instance. + # This will only differ from 'attributes' if a schema has been set. + + test "new model should have no known attributes" do + assert Person.known_attributes.blank?, "should have no known attributes" + assert Person.new.known_attributes.blank?, "should have no known attributes on a new instance" + end + + test "setting schema should set known attributes on class and instance" do + new_schema = {'age' => 'integer', 'name' => 'string'} + + assert_nothing_raised { Person.schema = new_schema } + + assert_equal new_schema.keys, Person.known_attributes + assert_equal new_schema.keys, Person.new.known_attributes + end + + test "known attributes on a fetched resource should return all the attributes of the instance" do + p = Person.find(1) + attrs = p.known_attributes + + assert attrs.present?, "should have found some attributes!" + + p.attributes.each do |the_attr, val| + assert attrs.include?(the_attr), "should have found attr: #{the_attr} in known attributes, but only had: #{attrs.inspect}" + end + end + + test "with two instances, known attributes should match the attributes of the individual instances - even if they differ" do + matz = Person.find(1) + rick = Person.find(5) + + m_attrs = matz.attributes.keys.sort + r_attrs = rick.attributes.keys.sort + + assert_not_equal m_attrs, r_attrs, "should have different attributes on each model" + + assert_not_equal matz.known_attributes, rick.known_attributes, "should have had different known attributes too" + end + + test "setting schema then fetching should add schema attributes to the intance attributes" do + # an attribute in common with fetched instance and one that isn't + new_schema = {'name' => 'string', 'my_strange_attribute' => 'string'} + + assert_nothing_raised { Person.schema = new_schema } + + matz = Person.find(1) + known_attrs = matz.known_attributes + + matz.attributes.keys.each do |the_attr| + assert known_attrs.include?(the_attr), "should have found instance attr: #{the_attr} in known attributes, but only had: #{known_attrs.inspect}" + end + new_schema.keys.each do |the_attr| + assert known_attrs.include?(the_attr), "should have found schema attr: #{the_attr} in known attributes, but only had: #{known_attrs.inspect}" + end + end + + +end -- cgit v1.2.3 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.rb | 3 +- activeresource/lib/active_resource/base.rb | 15 +++--- activeresource/lib/active_resource/schema.rb | 58 ++++++++++++++++++++++ .../lib/active_resource/schema_definition.rb | 58 ---------------------- activeresource/test/cases/base/schema_test.rb | 48 +++++++++--------- 5 files changed, 91 insertions(+), 91 deletions(-) create mode 100644 activeresource/lib/active_resource/schema.rb delete mode 100644 activeresource/lib/active_resource/schema_definition.rb (limited to 'activeresource') diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb index 84baf4227a..3e4a1dd4a1 100644 --- a/activeresource/lib/active_resource.rb +++ b/activeresource/lib/active_resource.rb @@ -37,7 +37,8 @@ module ActiveResource autoload :Connection autoload :CustomMethods autoload :Formats + autoload :HttpMock autoload :Observing + autoload :Schema autoload :Validations - autoload :HttpMock end diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 60bd573911..b39f8fbd48 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -13,7 +13,6 @@ require 'set' require 'uri' require 'active_resource/exceptions' -require 'active_resource/schema_definition' module ActiveResource # ActiveResource::Base is the main class for mapping RESTful resources as models in a Rails application. @@ -270,9 +269,9 @@ module ActiveResource # s.integer 'age' # s.float 'height', 'weight' # - # # unsupported types should be left as strings + # # unsupported types should be left as strings # # overload the accessor methods if you need to convert them - # s.attribute 'created_at', 'string' + # s.attribute 'created_at', 'string' # end # end # @@ -295,14 +294,14 @@ module ActiveResource # string, integer, float # # Note: at present the attribute-type doesn't do anything, but stay - # tuned... + # tuned... # Shortly it will also *cast* the value of the returned attribute. # ie: # j.age # => 34 # cast to an integer # j.weight # => '65' # still a string! # def define_schema - schema_definition = SchemaDefinition.new + schema_definition = Schema.new yield schema_definition if block_given? # skip out if we didn't define anything @@ -317,7 +316,7 @@ module ActiveResource end schema - end + end # Alternative, direct way to specify a schema for this @@ -326,7 +325,7 @@ module ActiveResource # # Pass the schema as a hash with the keys being the attribute-names # and the value being one of the accepted attribute types (as defined - # in define_schema) + # in define_schema) # # example: # @@ -342,7 +341,7 @@ module ActiveResource # purposefully nulling out the schema @schema = nil @known_attributes = [] - return + return end raise ArgumentError, "Expected a hash" unless the_schema.kind_of? Hash 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 diff --git a/activeresource/lib/active_resource/schema_definition.rb b/activeresource/lib/active_resource/schema_definition.rb deleted file mode 100644 index abcbf3ba4e..0000000000 --- a/activeresource/lib/active_resource/schema_definition.rb +++ /dev/null @@ -1,58 +0,0 @@ -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 diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb index 398e7cf539..4a6e8bfbdb 100644 --- a/activeresource/test/cases/base/schema_test.rb +++ b/activeresource/test/cases/base/schema_test.rb @@ -92,7 +92,7 @@ class SchemaTest < ActiveModel::TestCase test "schema should accept all known attribute types as values" do # I'd prefer to use here... - ActiveResource::SchemaDefinition::KNOWN_ATTRIBUTE_TYPES.each do |the_type| + ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type| assert_nothing_raised("should have accepted #{the_type.inspect}"){ Person.schema = {'my_key' => the_type }} end end @@ -171,13 +171,13 @@ class SchemaTest < ActiveModel::TestCase matz = Person.find(1) assert !matz.schema.blank?, "should have some sort of schema on an instance variable" assert_not_equal new_schema, matz.schema, "should not have the class-level schema until it's been added to the class!" - + assert_nothing_raised { Person.schema = new_schema assert_equal new_schema, matz.schema, "class-level schema should override instance-level schema" } end - + ##################################################### # Using the define_schema syntax @@ -188,11 +188,11 @@ class SchemaTest < ActiveModel::TestCase assert_nothing_raised("Should allow the define_schema to take a block") do Person.define_schema do |s| - assert s.kind_of?(ActiveResource::SchemaDefinition), "the 's' should be a schema definition or we're way off track..." + assert s.kind_of?(ActiveResource::Schema), "the 's' should be a schema definition or we're way off track..." end end end - + test "schema definition should store and return attribute set" do assert_nothing_raised do Person.define_schema do |s| @@ -202,13 +202,13 @@ class SchemaTest < ActiveModel::TestCase end end end - + test "should be able to add attributes through define_schema" do assert_nothing_raised do Person.define_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" + assert_equal 'string', s.attrs['foo'], "should have saved the attribute type" end end end @@ -218,23 +218,23 @@ class SchemaTest < ActiveModel::TestCase Person.define_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" + assert_equal 'integer', s.attrs['foo'], "should have saved the attribute type as a string" end end end - + test "should be able to add all known attribute types" do Person.define_schema do |s| - ActiveResource::SchemaDefinition::KNOWN_ATTRIBUTE_TYPES.each do |the_type| + 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}" + assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}" end end end end - + test "attributes should not accept unknown values" do bad_values = [ :oogle, :blob, 'thing'] @@ -251,20 +251,20 @@ class SchemaTest < ActiveModel::TestCase end end - + test "should accept attribute types as the type's name as the method" do Person.define_schema do |s| - ActiveResource::SchemaDefinition::KNOWN_ATTRIBUTE_TYPES.each do |the_type| + 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}" + assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}" end end end - + test "should accept multiple attribute names for an attribute method" do names = ['foo','bar','baz'] Person.define_schema do |s| @@ -273,7 +273,7 @@ class SchemaTest < ActiveModel::TestCase 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" + assert_equal 'string', s.attrs[the_name], "should have saved the attribute as a string" end end end @@ -282,13 +282,13 @@ class SchemaTest < ActiveModel::TestCase # What a schema does for us #### - # respond_to? + # respond_to? test "should respond positively to attributes that are only in the schema" do new_attr_name = :my_new_schema_attribute new_attr_name_two = :another_new_schema_attribute assert Person.schema.blank?, "sanity check - should have a blank class schema" - + assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" assert !Person.new.respond_do?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" @@ -306,7 +306,7 @@ class SchemaTest < ActiveModel::TestCase new_attr_name_two = :another_new_schema_attribute assert Person.schema.blank?, "sanity check - should have a blank class schema" - + assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" assert !Person.new.respond_do?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" @@ -328,10 +328,10 @@ class SchemaTest < ActiveModel::TestCase assert Person.schema.blank?, "sanity check - should have a blank class schema" assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name} as a method") do - Person.new.send(new_attr_name) + Person.new.send(new_attr_name) end assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name_two} as a method") do - Person.new.send(new_attr_name_two) + Person.new.send(new_attr_name_two) end Person.schema = {new_attr_name.to_s => :float} @@ -348,9 +348,9 @@ class SchemaTest < ActiveModel::TestCase # Known attributes # # Attributes can be known to be attributes even if they aren't actually - # 'set' on a particular instance. + # 'set' on a particular instance. # This will only differ from 'attributes' if a schema has been set. - + test "new model should have no known attributes" do assert Person.known_attributes.blank?, "should have no known attributes" assert Person.new.known_attributes.blank?, "should have no known attributes on a new instance" -- 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/base.rb | 43 +++++++++++++-------------- activeresource/lib/active_resource/schema.rb | 4 +-- activeresource/test/cases/base/schema_test.rb | 36 +++++++++++----------- 3 files changed, 40 insertions(+), 43 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index b39f8fbd48..d07571e1d7 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -241,12 +241,6 @@ module ActiveResource cattr_accessor :logger class << self - # This will shortly disappear to be replaced by the migration-style - # usage of this for defining a schema. At that point, all the doc - # currenlty on schema= will move back here... - def schema # :nodoc: - @schema ||= nil - end # Creates a schema for this resource - setting the attributes that are # known prior to fetching an instance from the remote system. # @@ -260,7 +254,7 @@ module ActiveResource # # example: # class Person < ActiveResource::Base - # define_schema do |s| + # schema do |s| # # define each attribute separately # s.attribute 'name', :string # @@ -300,32 +294,35 @@ module ActiveResource # j.age # => 34 # cast to an integer # j.weight # => '65' # still a string! # - def define_schema - schema_definition = Schema.new - yield schema_definition if block_given? + def schema(&block) + if block_given? + schema_definition = Schema.new + yield schema_definition - # skip out if we didn't define anything - return unless schema_definition.attrs.present? + # skip out if we didn't define anything + return unless schema_definition.attrs.present? - @schema ||= {}.with_indifferent_access - @known_attributes ||= [] + @schema ||= {}.with_indifferent_access + @known_attributes ||= [] - schema_definition.attrs.each do |k,v| - @schema[k] = v - @known_attributes << k - end + schema_definition.attrs.each do |k,v| + @schema[k] = v + @known_attributes << k + end - schema + schema + else + @schema ||= nil + end end - # Alternative, direct way to specify a schema for this - # Resource. define_schema is more flexible, but this is quick + # Resource. schema is more flexible, but this is quick # for a very simple schema. # # Pass the schema as a hash with the keys being the attribute-names # and the value being one of the accepted attribute types (as defined - # in define_schema) + # in schema) # # example: # @@ -346,7 +343,7 @@ module ActiveResource raise ArgumentError, "Expected a hash" unless the_schema.kind_of? Hash - define_schema do |s| + schema do |s| the_schema.each {|k,v| s.attribute(k,v) } end end 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 = {} diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb index 4a6e8bfbdb..98a8b98da1 100644 --- a/activeresource/test/cases/base/schema_test.rb +++ b/activeresource/test/cases/base/schema_test.rb @@ -180,14 +180,14 @@ class SchemaTest < ActiveModel::TestCase ##################################################### - # Using the define_schema syntax + # Using the schema syntax #### - test "should be able to use define_schema" do - assert Person.respond_to?(:define_schema), "should at least respond to the define_schema method" + test "should be able to use schema" do + assert Person.respond_to?(:schema), "should at least respond to the schema method" - assert_nothing_raised("Should allow the define_schema to take a block") do - Person.define_schema do |s| + 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 end @@ -195,7 +195,7 @@ class SchemaTest < ActiveModel::TestCase test "schema definition should store and return attribute set" do assert_nothing_raised do - Person.define_schema do |s| + 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") @@ -203,9 +203,9 @@ class SchemaTest < ActiveModel::TestCase end end - test "should be able to add attributes through define_schema" do + test "should be able to add attributes through schema" do assert_nothing_raised do - Person.define_schema do |s| + 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" @@ -215,7 +215,7 @@ class SchemaTest < ActiveModel::TestCase test "should convert symbol attributes to strings" do assert_nothing_raised do - Person.define_schema do |s| + 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" @@ -224,7 +224,7 @@ class SchemaTest < ActiveModel::TestCase end test "should be able to add all known attribute types" do - Person.define_schema do |s| + Person.schema do |s| 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}" @@ -238,7 +238,7 @@ class SchemaTest < ActiveModel::TestCase test "attributes should not accept unknown values" do bad_values = [ :oogle, :blob, 'thing'] - Person.define_schema do |s| + 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 @@ -253,7 +253,7 @@ class SchemaTest < ActiveModel::TestCase test "should accept attribute types as the type's name as the method" do - Person.define_schema do |s| + 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 @@ -267,7 +267,7 @@ class SchemaTest < ActiveModel::TestCase test "should accept multiple attribute names for an attribute method" do names = ['foo','bar','baz'] - Person.define_schema do |s| + Person.schema do |s| assert_nothing_raised("should take strings with multiple attribute names as params") do s.string( *names) end @@ -294,11 +294,11 @@ class SchemaTest < ActiveModel::TestCase assert_nothing_raised do Person.schema = {new_attr_name.to_s => 'string'} - Person.define_schema {|s| s.string new_attr_name_two } + Person.schema {|s| s.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}" - assert Person.new.respond_to?(new_attr_name_two), "should respond to the attribute from the define_schema, but failed on: #{new_attr_name_two}" + assert Person.new.respond_to?(new_attr_name_two), "should respond to the attribute from the schema, but failed on: #{new_attr_name_two}" end test "should not care about ordering of schema definitions" do @@ -311,12 +311,12 @@ 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.define_schema {|s| s.string new_attr_name_two } + Person.schema {|s| s.string new_attr_name_two } Person.schema = {new_attr_name.to_s => 'string'} 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}" - assert Person.new.respond_to?(new_attr_name_two), "should respond to the attribute from the define_schema, but failed on: #{new_attr_name_two}" + assert Person.new.respond_to?(new_attr_name_two), "should respond to the attribute from the schema, but failed on: #{new_attr_name_two}" end # method_missing effects @@ -335,7 +335,7 @@ class SchemaTest < ActiveModel::TestCase end Person.schema = {new_attr_name.to_s => :float} - Person.define_schema {|s| s.string new_attr_name_two } + Person.schema {|s| s.string new_attr_name_two } assert_nothing_raised do Person.new.send(new_attr_name) -- 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/base.rb | 18 ++--- activeresource/lib/active_resource/schema.rb | 11 +-- activeresource/test/cases/base/schema_test.rb | 102 ++++++++++++++------------ 3 files changed, 69 insertions(+), 62 deletions(-) (limited to 'activeresource') 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) -- 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') 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 From ace20bd25e3818b7f29c222643dd445c48b36425 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 22 Dec 2009 17:27:37 -0600 Subject: Flip deferrable autoload convention --- activeresource/lib/active_resource.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb index 3e4a1dd4a1..e0a6ecbcce 100644 --- a/activeresource/lib/active_resource.rb +++ b/activeresource/lib/active_resource.rb @@ -33,12 +33,14 @@ require 'active_model' module ActiveResource extend ActiveSupport::Autoload - autoload :Base - autoload :Connection - autoload :CustomMethods - autoload :Formats - autoload :HttpMock - autoload :Observing - autoload :Schema - autoload :Validations + eager_autoload do + autoload :Base + autoload :Connection + autoload :CustomMethods + autoload :Formats + autoload :HttpMock + autoload :Observing + autoload :Schema + autoload :Validations + end end -- cgit v1.2.3 From 22752ec27c4eeb50ec12ed2f147f1c066062cabd Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 22 Dec 2009 17:36:29 -0600 Subject: All ARes modules are safe to defer --- activeresource/lib/active_resource.rb | 18 ++++++++---------- activeresource/lib/active_resource/base.rb | 3 +++ 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb index e0a6ecbcce..3e4a1dd4a1 100644 --- a/activeresource/lib/active_resource.rb +++ b/activeresource/lib/active_resource.rb @@ -33,14 +33,12 @@ require 'active_model' module ActiveResource extend ActiveSupport::Autoload - eager_autoload do - autoload :Base - autoload :Connection - autoload :CustomMethods - autoload :Formats - autoload :HttpMock - autoload :Observing - autoload :Schema - autoload :Validations - end + autoload :Base + autoload :Connection + autoload :CustomMethods + autoload :Formats + autoload :HttpMock + autoload :Observing + autoload :Schema + autoload :Validations end diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index b833e9c8ce..a6243e7011 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -13,6 +13,9 @@ require 'set' require 'uri' require 'active_resource/exceptions' +require 'active_resource/connection' +require 'active_resource/formats' +require 'active_resource/schema' module ActiveResource # ActiveResource::Base is the main class for mapping RESTful resources as models in a Rails application. -- cgit v1.2.3 From 46b376962f064077734773c7e1eea5881e5d5696 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 23 Dec 2009 23:30:59 -0800 Subject: Fix new schema test dependency on Hash#to_xml --- activeresource/test/cases/base/schema_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activeresource') diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb index d1afb9f439..d9dc679941 100644 --- a/activeresource/test/cases/base/schema_test.rb +++ b/activeresource/test/cases/base/schema_test.rb @@ -1,4 +1,5 @@ require 'abstract_unit' +require 'active_support/core_ext/hash/conversions' require "fixtures/person" require "fixtures/street_address" -- cgit v1.2.3