Permalink
Browse files

Rename define_schema => schema

  • Loading branch information...
1 parent 669c5ee commit c0ad3f6cc618f42eae0c5d5ceefde32ff3342c20 @josh josh committed Dec 21, 2009
@@ -241,12 +241,6 @@ class Base
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 <tt>schema=</tt> 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 @@ def schema # :nodoc:
#
# example:
# class Person < ActiveResource::Base
- # define_schema do |s|
+ # schema do |s|
# # define each attribute separately
# s.attribute 'name', :string
#
@@ -300,32 +294,35 @@ def schema # :nodoc:
# 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 <tt>schema</tt> for this
- # Resource. <tt>define_schema</tt> is more flexible, but this is quick
+ # Resource. <tt>schema</tt> 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 <tt>define_schema</tt>)
+ # in <tt>schema</tt>)
#
# example:
#
@@ -346,7 +343,7 @@ def schema=(the_schema)
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
@@ -15,13 +15,13 @@ class Schema # :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 = {}
@@ -180,32 +180,32 @@ def teardown
#####################################################
- # 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
end
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")
end
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 @@ def teardown
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 @@ def teardown
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 @@ def teardown
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 @@ def teardown
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 @@ def teardown
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 @@ def teardown
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 @@ def teardown
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 @@ def teardown
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)

1 comment on commit c0ad3f6

@taryneast

This was top of my TODO list. Guess I don't need to now ;)

Please sign in to comment.