Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
bf3332bf
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,发现更多精彩内容 >>
提交
bf3332bf
编写于
6月 21, 2012
作者:
F
Francesco Rodriguez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update ActiveModel::AttributeMethods documentation [ci skip]
上级
84213613
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
20 addition
and
27 deletion
+20
-27
activemodel/lib/active_model/attribute_methods.rb
activemodel/lib/active_model/attribute_methods.rb
+20
-27
未找到文件。
activemodel/lib/active_model/attribute_methods.rb
浏览文件 @
bf3332bf
...
...
@@ -7,28 +7,27 @@ class MissingAttributeError < NoMethodError
end
# == Active Model Attribute Methods
#
# <tt>ActiveModel::AttributeMethods</tt> provides a way to add prefixes and
suffixes
#
to your methods as well as handling the creation of Active Record like class methods
# such as +table_name+.
# <tt>ActiveModel::AttributeMethods</tt> provides a way to add prefixes and
#
suffixes to your methods as well as handling the creation of Active Record
#
like class methods
such as +table_name+.
#
# The requirements to implement ActiveModel::AttributeMethods are to:
#
# * <tt>include ActiveModel::AttributeMethods</tt> in your object
# * <tt>include ActiveModel::AttributeMethods</tt> in your object
.
# * Call each Attribute Method module method you want to add, such as
# attribute_method_suffix or attribute_method_prefix
# * Call <tt>define_attribute_methods</tt> after the other methods are
# called.
# * Define the various generic +_attribute+ methods that you have declared
# +attribute_method_suffix+ or +attribute_method_prefix+.
# * Call +define_attribute_methods+ after the other methods are called.
# * Define the various generic +_attribute+ methods that you have declared.
#
# A minimal implementation could be:
#
# class Person
# include ActiveModel::AttributeMethods
#
# attribute_method_affix
:prefix => 'reset_', :suffix =>
'_to_default!'
# attribute_method_affix
prefix: 'reset_', suffix:
'_to_default!'
# attribute_method_suffix '_contrived?'
# attribute_method_prefix 'clear_'
# define_attribute_methods
'name'
# define_attribute_methods
:name
#
# attr_accessor :name
#
...
...
@@ -43,12 +42,12 @@ class MissingAttributeError < NoMethodError
# end
#
# def reset_attribute_to_default!(attr)
# send("#{attr}=",
"Default Name"
)
# send("#{attr}=",
'Default Name'
)
# end
# end
#
# Note that whenever you include ActiveModel::AttributeMethods in your class,
# it requires you to implement an
<tt>attributes</tt>
method which returns a hash
# it requires you to implement an
+attributes+
method which returns a hash
# with each attribute name in your model as hash key and the attribute value as
# hash value.
#
...
...
@@ -78,8 +77,6 @@ module ClassMethods
# An instance method <tt>#{prefix}attribute</tt> must exist and accept
# at least the +attr+ argument.
#
# For example:
#
# class Person
#
# include ActiveModel::AttributeMethods
...
...
@@ -95,7 +92,7 @@ module ClassMethods
# end
#
# person = Person.new
# person.name =
"Bob"
# person.name =
'Bob'
# person.name # => "Bob"
# person.clear_name
# person.name # => nil
...
...
@@ -113,10 +110,8 @@ def attribute_method_prefix(*prefixes)
#
# attribute#{suffix}(#{attr}, *args, &block)
#
# An <tt>attribute#{suffix}</tt> instance method must exist and accept at least
# the +attr+ argument.
#
# For example:
# An <tt>attribute#{suffix}</tt> instance method must exist and accept at
# least the +attr+ argument.
#
# class Person
#
...
...
@@ -133,7 +128,7 @@ def attribute_method_prefix(*prefixes)
# end
#
# person = Person.new
# person.name =
"Bob"
# person.name =
'Bob'
# person.name # => "Bob"
# person.name_short? # => true
def
attribute_method_suffix
(
*
suffixes
)
...
...
@@ -154,13 +149,11 @@ def attribute_method_suffix(*suffixes)
# An <tt>#{prefix}attribute#{suffix}</tt> instance method must exist and
# accept at least the +attr+ argument.
#
# For example:
#
# class Person
#
# include ActiveModel::AttributeMethods
# attr_accessor :name
# attribute_method_affix
:prefix => 'reset_', :suffix =>
'_to_default!'
# attribute_method_affix
prefix: 'reset_', suffix:
'_to_default!'
# define_attribute_methods :name
#
# private
...
...
@@ -188,7 +181,7 @@ def attribute_method_affix(*affixes)
# end
#
# person = Person.new
# person.nickname =
"Bob"
# person.nickname =
'Bob'
# person.nickname # => "Bob"
# person.name # => "Bob"
def
alias_attribute
(
new_name
,
old_name
)
...
...
@@ -202,9 +195,9 @@ def alias_attribute(new_name, old_name)
# Declares the attributes that should be prefixed and suffixed by
# ActiveModel::AttributeMethods.
#
# To use, pass
in an array of attribute names (as strings or symbols),
#
be sure to declare +define_attribute_methods+ after you define any
#
prefix, suffix or affix
methods, or they will not hook in.
# To use, pass
attribute names (as strings or symbols), be sure to declare
#
+define_attribute_methods+ after you define any prefix, suffix or affix
# methods, or they will not hook in.
#
# class Person
#
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录