|
|
|
|
|
by zimbatm
4953 days ago
|
|
I think that we agree on the bottom-line: UserContentSpamChecker has no need for a state and is just some kind of namespace. That's the point that I wanted to get trough. Then we can argue on the best way to handle that namespace. I'm not particularly fond of the class method approach neither but I don't think that your approach is appropriate either. A namespace should be instantiated only once in my opinion, that's why I'm going for the singleton object. Maybe the problem is with ruby and it should provide another mechanism for managing namespaces ? UserContentSpamChecker = namespace do
TRIGGER_KEYWORDS = %w(viagra acne adult loans xrated).to_set
def is_spam?(content)
flagged_words(content).present?
end
protected
def flagged_words(content)
TRIGGER_KEYWORDS & content.split
end
end
class MyOtherClass
import :spam_checker, UserContentSpamChecker
def foo
spam_checker.is_spam?(content)
end
end
|
|
So for all practical purposes, if you define a module it is not much different than if you define a class, and then instantiate a single object (it is slightly different in that your object will be an instance of your class rather than of the class Class).
Modules are namespaces for Ruby (and pretty much only differs from classes in that you can't create instances of modules)
What you describe above is done with modules:
If you want to be able to alias it, you'd do it with a method: (or you could do it with a class variable or class instance variable - example class variable:)