aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/lib/active_resource
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-12-20 19:03:22 -0600
committerJoshua Peek <josh@joshpeek.com>2009-12-20 19:04:53 -0600
commit2e9c7759984573944592fb1bda5aeb7c58edba55 (patch)
treec07c471dee18345a6a5bbefb3e44d7878339918e /activeresource/lib/active_resource
parentc0ad3f6cc618f42eae0c5d5ceefde32ff3342c20 (diff)
downloadrails-2e9c7759984573944592fb1bda5aeb7c58edba55.tar.gz
rails-2e9c7759984573944592fb1bda5aeb7c58edba55.tar.bz2
rails-2e9c7759984573944592fb1bda5aeb7c58edba55.zip
Use instance_eval for schema block
Diffstat (limited to 'activeresource/lib/active_resource')
-rw-r--r--activeresource/lib/active_resource/base.rb18
-rw-r--r--activeresource/lib/active_resource/schema.rb11
2 files changed, 13 insertions, 16 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