Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
a521a3c0
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,发现更多精彩内容 >>
提交
a521a3c0
编写于
9月 15, 2010
作者:
E
Emilio Tagua
提交者:
José Valim
9月 17, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove TODO and support all expected attributes.
Signed-off-by:
N
José Valim
<
jose.valim@gmail.com
>
上级
3480551e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
47 addition
and
13 deletion
+47
-13
activeresource/lib/active_resource/schema.rb
activeresource/lib/active_resource/schema.rb
+1
-3
activeresource/test/cases/base/schema_test.rb
activeresource/test/cases/base/schema_test.rb
+46
-10
未找到文件。
activeresource/lib/active_resource/schema.rb
浏览文件 @
a521a3c0
...
@@ -4,7 +4,7 @@ module ActiveResource # :nodoc:
...
@@ -4,7 +4,7 @@ module ActiveResource # :nodoc:
class
Schema
# :nodoc:
class
Schema
# :nodoc:
# attributes can be known to be one of these types. They are easy to
# attributes can be known to be one of these types. They are easy to
# cast to/from.
# cast to/from.
KNOWN_ATTRIBUTE_TYPES
=
%w( string
integer float
)
KNOWN_ATTRIBUTE_TYPES
=
%w( string
text integer float decimal datetime timestamp time date binary boolean
)
# An array of attribute definitions, representing the attributes that
# An array of attribute definitions, representing the attributes that
# have been defined.
# have been defined.
...
@@ -39,8 +39,6 @@ def attribute(name, type, options = {})
...
@@ -39,8 +39,6 @@ def attribute(name, type, options = {})
# The following are the attribute types supported by Active Resource
# The following are the attribute types supported by Active Resource
# migrations.
# 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
|
KNOWN_ATTRIBUTE_TYPES
.
each
do
|
attr_type
|
class_eval
<<-
EOV
,
__FILE__
,
__LINE__
+
1
class_eval
<<-
EOV
,
__FILE__
,
__LINE__
+
1
def
#{
attr_type
.
to_s
}
(*args)
def
#{
attr_type
.
to_s
}
(*args)
...
...
activeresource/test/cases/base/schema_test.rb
浏览文件 @
a521a3c0
...
@@ -78,14 +78,23 @@ def teardown
...
@@ -78,14 +78,23 @@ def teardown
end
end
test
"schema should accept a simple hash"
do
test
"schema should accept a simple hash"
do
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
}
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
,
'height'
=>
'float'
,
'bio'
=>
'text'
,
'weight'
=>
'decimal'
,
'photo'
=>
'binary'
,
'alive'
=>
'boolean'
,
'created_at'
=>
'timestamp'
,
'thetime'
=>
'time'
,
'thedate'
=>
'date'
,
'mydatetime'
=>
'datetime'
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
assert_equal
new_schema
,
Person
.
schema
assert_equal
new_schema
,
Person
.
schema
end
end
test
"schema should accept a hash with simple values"
do
test
"schema should accept a hash with simple values"
do
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
,
'height'
=>
'float'
,
'mydatetime'
=>
'string'
}
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
,
'height'
=>
'float'
,
'bio'
=>
'text'
,
'weight'
=>
'decimal'
,
'photo'
=>
'binary'
,
'alive'
=>
'boolean'
,
'created_at'
=>
'timestamp'
,
'thetime'
=>
'time'
,
'thedate'
=>
'date'
,
'mydatetime'
=>
'datetime'
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
assert_equal
new_schema
,
Person
.
schema
assert_equal
new_schema
,
Person
.
schema
...
@@ -109,7 +118,12 @@ def teardown
...
@@ -109,7 +118,12 @@ def teardown
end
end
test
"schema should accept nil and remove the schema"
do
test
"schema should accept nil and remove the schema"
do
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
}
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
,
'height'
=>
'float'
,
'bio'
=>
'text'
,
'weight'
=>
'decimal'
,
'photo'
=>
'binary'
,
'alive'
=>
'boolean'
,
'created_at'
=>
'timestamp'
,
'thetime'
=>
'time'
,
'thedate'
=>
'date'
,
'mydatetime'
=>
'datetime'
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
assert_equal
new_schema
,
Person
.
schema
# sanity check
assert_equal
new_schema
,
Person
.
schema
# sanity check
...
@@ -120,7 +134,12 @@ def teardown
...
@@ -120,7 +134,12 @@ def teardown
test
"schema should be with indifferent access"
do
test
"schema should be with indifferent access"
do
new_schema
=
{
:age
=>
:integer
,
'name'
=>
'string'
}
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
,
'height'
=>
'float'
,
'bio'
=>
'text'
,
'weight'
=>
'decimal'
,
'photo'
=>
'binary'
,
'alive'
=>
'boolean'
,
'created_at'
=>
'timestamp'
,
'thetime'
=>
'time'
,
'thedate'
=>
'date'
,
'mydatetime'
=>
'datetime'
}
new_schema_syms
=
new_schema
.
keys
new_schema_syms
=
new_schema
.
keys
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
...
@@ -156,7 +175,12 @@ def teardown
...
@@ -156,7 +175,12 @@ def teardown
test
"defining a schema should return it when asked"
do
test
"defining a schema should return it when asked"
do
assert
Person
.
schema
.
blank?
,
"should have a blank class schema"
assert
Person
.
schema
.
blank?
,
"should have a blank class schema"
new_schema
=
{
'name'
=>
'string'
,
'age'
=>
'integer'
,
'height'
=>
'float'
,
'weight'
=>
'float'
}
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
,
'height'
=>
'float'
,
'bio'
=>
'text'
,
'weight'
=>
'decimal'
,
'photo'
=>
'binary'
,
'alive'
=>
'boolean'
,
'created_at'
=>
'timestamp'
,
'thetime'
=>
'time'
,
'thedate'
=>
'date'
,
'mydatetime'
=>
'datetime'
}
assert_nothing_raised
{
assert_nothing_raised
{
Person
.
schema
=
new_schema
Person
.
schema
=
new_schema
assert_equal
new_schema
,
Person
.
schema
,
"should have saved the schema on the class"
assert_equal
new_schema
,
Person
.
schema
,
"should have saved the schema on the class"
...
@@ -167,7 +191,11 @@ def teardown
...
@@ -167,7 +191,11 @@ def teardown
test
"defining a schema, then fetching a model should still match the defined schema"
do
test
"defining a schema, then fetching a model should still match the defined schema"
do
# sanity checks
# sanity checks
assert
Person
.
schema
.
blank?
,
"should have a blank class schema"
assert
Person
.
schema
.
blank?
,
"should have a blank class schema"
new_schema
=
{
'name'
=>
'string'
,
'age'
=>
'integer'
,
'height'
=>
'float'
,
'weight'
=>
'float'
}
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
,
'height'
=>
'float'
,
'bio'
=>
'text'
,
'weight'
=>
'decimal'
,
'photo'
=>
'binary'
,
'alive'
=>
'boolean'
,
'created_at'
=>
'timestamp'
,
'thetime'
=>
'time'
,
'thedate'
=>
'date'
,
'mydatetime'
=>
'datetime'
}
matz
=
Person
.
find
(
1
)
matz
=
Person
.
find
(
1
)
assert
!
matz
.
schema
.
blank?
,
"should have some sort of schema on an instance variable"
assert
!
matz
.
schema
.
blank?
,
"should have some sort of schema on an instance variable"
...
@@ -368,12 +396,16 @@ def teardown
...
@@ -368,12 +396,16 @@ def teardown
end
end
test
"setting schema should set known attributes on class and instance"
do
test
"setting schema should set known attributes on class and instance"
do
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
}
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
,
'height'
=>
'float'
,
'bio'
=>
'text'
,
'weight'
=>
'decimal'
,
'photo'
=>
'binary'
,
'alive'
=>
'boolean'
,
'created_at'
=>
'timestamp'
,
'thetime'
=>
'time'
,
'thedate'
=>
'date'
,
'mydatetime'
=>
'datetime'
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
assert_equal
new_schema
.
keys
,
Person
.
known_attributes
assert_equal
new_schema
.
keys
.
sort
,
Person
.
known_attributes
.
sort
assert_equal
new_schema
.
keys
,
Person
.
new
.
known_attributes
assert_equal
new_schema
.
keys
.
sort
,
Person
.
new
.
known_attributes
.
sort
end
end
test
"known attributes on a fetched resource should return all the attributes of the instance"
do
test
"known attributes on a fetched resource should return all the attributes of the instance"
do
...
@@ -401,7 +433,11 @@ def teardown
...
@@ -401,7 +433,11 @@ def teardown
test
"setting schema then fetching should add schema attributes to the intance attributes"
do
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
# an attribute in common with fetched instance and one that isn't
new_schema
=
{
'name'
=>
'string'
,
'my_strange_attribute'
=>
'string'
}
new_schema
=
{
'age'
=>
'integer'
,
'name'
=>
'string'
,
'height'
=>
'float'
,
'bio'
=>
'text'
,
'weight'
=>
'decimal'
,
'photo'
=>
'binary'
,
'alive'
=>
'boolean'
,
'created_at'
=>
'timestamp'
,
'thetime'
=>
'time'
,
'thedate'
=>
'date'
,
'mydatetime'
=>
'datetime'
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
assert_nothing_raised
{
Person
.
schema
=
new_schema
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录