module Aldous::Service::Result::Base::PredicateMethodsForInheritance
Public Instance Methods
inherited(child)
click to toggle source
For every child class, create a predicate method on the base named after the child that returns false and override the same predicate method on the child to return true. So if the child class is called Failure
then we’ll have a predicate method called failure? which will return false on the base class and true on the actual child class.
# File lib/aldous/service/result/base/predicate_methods_for_inheritance.rb, line 13 def inherited(child) return if child.name == nil # unnamed class child_class_name_as_predicate = "#{underscore(child.name.split("::").last)}?" add_predicate_method_to_class(Aldous::Service::Result::Base, child_class_name_as_predicate, false) add_predicate_method_to_class(child, child_class_name_as_predicate, true) end
Private Instance Methods
add_predicate_method_to_class(klass, method_name, method_value)
click to toggle source
# File lib/aldous/service/result/base/predicate_methods_for_inheritance.rb, line 23 def add_predicate_method_to_class(klass, method_name, method_value) unless klass.instance_methods(false).include?(method_name) klass.class_eval do define_method method_name do method_value end end end end
underscore(string)
click to toggle source
# File lib/aldous/service/result/base/predicate_methods_for_inheritance.rb, line 33 def underscore(string) string.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end