最近在开发token系统的过程中,需要在数据库中定义许多boolean值的字段,用来验证用户的这个token时候具有xx操作权限,按照ruby的惯例,一般不会如下来定义类的。
# Table name: tokens
#
# id
# token_hash
# read :boolean
# write :boolean
# ...
class Token < ActiveRecord::Base
def read?
read
end
def write?
write
end
end
这样定义不符合ruby的DRY原则。那么又该如何设计呢?
ruby有一个method_missing 的话,当你调用 Token.new.read? 方法时,该方法并没有定义,ruby就会调用 method_missing 方法,实现如下。
# 字段同上
class Token < ActiveRecord::Base
FIELDS = ["read", "write"]
def method_missing(m, *args, &block)
if FIELDS.include?(m[0...-1]) && m[-1] == "?"
self.send(m[0...-1], *args, &block)
else
raise NoMethodError.new("undefined method '#{m}' for #{inspect}:#{self.class}")
end
end
def respond_to?(m, include_private = false)
if if FIELDS.include?(m[0...-1]) && m[-1] == "?"
true
else
super
end
end
end
method_missing写法又不太优雅,而且send调用方法比调用已定义方法慢很多。
ruby一个吸引人的地方在于它的元编程,可以动态的改变类和对象的结构。可以在类定义时使用define_method给类添加方法。
来使用define_method改良一下上述method_missing慢的问题。
# 字段同上
class Token < ActiveRecord::Base
FIELDS = ["read", "write"]
def method_missing(m, *args, &block)
if FIELDS.include?(m[0...-1]) && m[-1] == "?"
self.class.send :define_method, m do
self.send(m[0...-1])
end
self.send(m)
end
end
end
改良之后更加觉得别扭。
直接使用define_method来定义类。
# 字段同上
class Token < ActiveRecord::Base
FIELDS = [:read, :write]
FIELDS.each do |name|
define_method("#{name}?") do
send(name)
end
end
end
在rails中有更方便的定义属性方法的方式。使用define_attribute_methods 来定义属性方法,这真是棒极了。
# 字段同上
class Token < ActiveRecord::Base
inlcude ActvieModel::AttributeMethods
attribute_method_suffix '?'
define_attribute_methods = [:read, :write]
end
还有你不仅可以通过这种方式定义后缀属性方法,还可以定义前缀属性方法甚至有前缀和后缀的属性方法。如attribute_method_prefix 或者attribute_method_affix。
# 字段同上
class Token < ActiveRecord::Base
inlcude ActvieModel::AttributeMethods
attribute_method_affix prefix: 'enable_', suffix: '?'
define_attribute_methods [:read, :write]
end
Token.new.enable_read?
值得注意的是,在调用define_attribute_methods之前必须有attribute_method_prefix, attribute_method_suffix或attribute_method_affix声明。