Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
669c5eec
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,发现更多精彩内容 >>
提交
669c5eec
编写于
12月 20, 2009
作者:
J
Joshua Peek
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename SchemaDefinition => Schema
上级
fc9b3e4a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
37 addition
and
37 deletion
+37
-37
activeresource/lib/active_resource.rb
activeresource/lib/active_resource.rb
+2
-1
activeresource/lib/active_resource/base.rb
activeresource/lib/active_resource/base.rb
+7
-8
activeresource/lib/active_resource/schema.rb
activeresource/lib/active_resource/schema.rb
+4
-4
activeresource/test/cases/base/schema_test.rb
activeresource/test/cases/base/schema_test.rb
+24
-24
未找到文件。
activeresource/lib/active_resource.rb
浏览文件 @
669c5eec
...
...
@@ -37,7 +37,8 @@ module ActiveResource
autoload
:Connection
autoload
:CustomMethods
autoload
:Formats
autoload
:HttpMock
autoload
:Observing
autoload
:Schema
autoload
:Validations
autoload
:HttpMock
end
activeresource/lib/active_resource/base.rb
浏览文件 @
669c5eec
...
...
@@ -13,7 +13,6 @@
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 @@ def schema # :nodoc:
# 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 @@ def schema # :nodoc:
# 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
=
Schema
Definition
.
new
schema_definition
=
Schema
.
new
yield
schema_definition
if
block_given?
# skip out if we didn't define anything
...
...
@@ -317,7 +316,7 @@ def define_schema
end
schema
end
end
# Alternative, direct way to specify a <tt>schema</tt> for this
...
...
@@ -326,7 +325,7 @@ def define_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>define_schema</tt>)
#
# example:
#
...
...
@@ -342,7 +341,7 @@ def schema=(the_schema)
# purposefully nulling out the schema
@schema
=
nil
@known_attributes
=
[]
return
return
end
raise
ArgumentError
,
"Expected a hash"
unless
the_schema
.
kind_of?
Hash
...
...
activeresource/lib/active_resource/schema
_definition
.rb
→
activeresource/lib/active_resource/schema.rb
浏览文件 @
669c5eec
require
'active_resource/exceptions'
module
ActiveResource
# :nodoc:
class
Schema
Definition
# :nodoc:
class
Schema
# :nodoc:
# attributes can be known to be one of these types. They are easy to
# cast to/from.
...
...
@@ -11,7 +11,7 @@ class SchemaDefinition # :nodoc:
# have been defined.
attr_accessor
:attrs
# The internals of an Active Resource Schema
Definition
are very simple -
# 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:
...
...
@@ -28,7 +28,7 @@ def initialize
end
def
attribute
(
name
,
type
,
options
=
{})
raise
ArgumentError
,
"Unknown Attribute type:
#{
type
.
inspect
}
for key:
#{
name
.
inspect
}
"
unless
type
.
nil?
||
Schema
Definition
::
KNOWN_ATTRIBUTE_TYPES
.
include?
(
type
.
to_s
)
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
...
...
@@ -39,7 +39,7 @@ def attribute(name, type, options = {})
end
# 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
|
...
...
activeresource/test/cases/base/schema_test.rb
浏览文件 @
669c5eec
...
...
@@ -92,7 +92,7 @@ def teardown
test
"schema should accept all known attribute types as values"
do
# I'd prefer to use here...
ActiveResource
::
Schema
Definition
::
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 @@ def teardown
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 @@ def teardown
assert_nothing_raised
(
"Should allow the define_schema to take a block"
)
do
Person
.
define_schema
do
|
s
|
assert
s
.
kind_of?
(
ActiveResource
::
Schema
Definition
),
"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 @@ def teardown
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 @@ def teardown
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
::
Schema
Definition
::
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 @@ def teardown
end
end
test
"should accept attribute types as the type's name as the method"
do
Person
.
define_schema
do
|
s
|
ActiveResource
::
Schema
Definition
::
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 @@ def teardown
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 @@ def teardown
# 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 @@ def teardown
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 @@ def teardown
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 @@ def teardown
# 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"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录