Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
2e9c7759
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2e9c7759
编写于
12月 20, 2009
作者:
J
Joshua Peek
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use instance_eval for schema block
上级
c0ad3f6c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
69 addition
and
62 deletion
+69
-62
activeresource/lib/active_resource/base.rb
activeresource/lib/active_resource/base.rb
+9
-9
activeresource/lib/active_resource/schema.rb
activeresource/lib/active_resource/schema.rb
+4
-7
activeresource/test/cases/base/schema_test.rb
activeresource/test/cases/base/schema_test.rb
+56
-46
未找到文件。
activeresource/lib/active_resource/base.rb
浏览文件 @
2e9c7759
...
...
@@ -254,18 +254,18 @@ class << self
#
# 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
.s
tring '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 @@ class << self
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 @@ def schema=(the_schema)
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
...
...
activeresource/lib/active_resource/schema.rb
浏览文件 @
2e9c7759
...
...
@@ -2,7 +2,6 @@
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 @@ 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:
# schema do
|s|
# s
.s
tring :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 @@ def attribute(name, type, options = {})
# %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
activeresource/test/cases/base/schema_test.rb
浏览文件 @
2e9c7759
...
...
@@ -187,50 +187,57 @@ def teardown
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
a
ssert_equal
({
'foo'
=>
'string'
},
s
.
attrs
,
"should return attributes in practice"
)
s
=
nil
Person
.
schema
do
s
=
self
a
ttribute
: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"
a
ssert_equal
'string'
,
s
.
attrs
[
'foo'
],
"should have saved the attribute type"
s
=
nil
Person
.
schema
do
s
=
self
a
ttribute
(
'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"
a
ssert
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
a
ttribute
(
: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"
a
ssert_equal
the_type
.
to_s
,
s
.
attrs
[
'foo'
],
"should have saved the attribute type of:
#{
the_type
}
"
s
=
nil
Person
.
schema
do
s
=
self
a
ttribute
(
'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 @@ def teardown
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 @@ def teardown
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 @@ def teardown
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 @@ 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
.
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 @@ def teardown
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
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录