创建一个引发类特定错误的模块 [英] Creating a module for raising class-specific errors
问题描述
在我的rails项目中,我经常在类和模型中使用这种行为:
In my rails projects, I often use this sort of behavior in my classes and models:
class Whatever
class WhateverError < StandardError; end
def initialize(params={})
raise WhateverError.new("Bad params: #{params}") if condition
# actual class code to follow
end
end
麻烦的是,这既重复又很冗长.如果可以在需要引发特定于类的错误时执行此操作,我将非常喜欢:
The trouble is, this is both hugely repetitive and fairly verbose. I'd love it if I could just do this whenever I need to raise a class-specific error:
class ErrorRaiser
include ClassErrors
def initialize(params={})
error("Bad params: #{params}") if condition
error if other_condition # has default message
# actual class code to follow
end
def self.class_method
error if third_condition # class method, behaves identically
end
end
我在创建这样的模块时遇到了很大的麻烦.我早期的尝试很可悲,如下所示,但是我对模块范围内的可用内容,如何动态创建类(在方法内?)或是否具有简单的.
I'm having major trouble creating such a module. My sad early attempts have tended to look something like the below, but I'm pretty confused about what's available within the scope of the module, how to dynamically create classes (within methods?) or whether I have straightforward access to the "calling" class at all.
我的基本要求是,error
既要是类方法又要是实例方法,必须被命名空间"给调用它的类,并且要有默认消息.有什么想法/帮助吗?这有可能吗?
My basic requirements are that error
be both a class method and an instance method, that it be "namespaced" to the class calling it, and that it have a default message. Any thoughts/help? Is this even possible?
module ClassErrorable
# This and the "extend" bit (theoretically) allow error to be a class method as well
module ClassMethods
def self.error(string=nil)
ClassErrorable.new(string).error
end
end
def self.included(base)
set_error_class(base)
base.extend ClassMethods
end
def self.set_error_class(base)
# I'm shaky on the scoping. Do I refer to this with @ in a class method
# but @@ in an instance method? Should I define it here with @ then?
@@error_class = "##{base.class}Error".constantize
end
def self.actual_error
# This obviously doesn't work, and in fact,
# it raises a syntax error. How can I make my
# constant a class inheriting from StandardError?
@@actual_error = @@error_class < StandardError; end
end
def initialize(string)
@string = string || "There's been an error!"
end
def error(string=nil)
raise @@actual_error.new(string)
end
end
推荐答案
这样的事情(用纯Ruby编写;可以重构为使用一些特定于Rails的功能,例如.constantize):
How about something like this (written in pure Ruby; it could be refactored to use some Rails-specific features like .constantize):
module ClassErrorable
module ClassMethods
def error(message = nil)
klass = Object::const_get(exception_class_name)
raise klass.new(message || "There's been an error!")
end
def exception_class_name
name + 'Error'
end
end
def self.included(base)
base.extend ClassMethods
Object::const_set(base.exception_class_name, Class.new(Exception))
end
def error(message = nil)
self.class.error(message)
end
end
这篇关于创建一个引发类特定错误的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!