module Wallaby::ModuleUtils
Utils
for module and class
Public Class Methods
Check whether a class is anonymous or not @param klass [Class] @return [true] if a class is anonymous @return [false] otherwise
# File lib/utils/wallaby/module_utils.rb, line 23 def anonymous_class?(klass) klass.name.blank? || klass.to_s.start_with?('#<Class') end
Check if a child class inherits from parent class @param child [Class] child class @param parent [Class] parent class @raise [ArgumentError] if given class is not a child of the other class
# File lib/utils/wallaby/module_utils.rb, line 31 def inheritance_check(child, parent) return unless child && parent return if child < parent raise ::ArgumentError, Locale.t('errors.invalid.inheritance', klass: child, parent: parent) end
A helper method to check if subject responds to given method and to return the result if so @param subject [Object] @param method_id [String, Symbol] @param args [Array] a list of arguments @return [Object] result from executing given method on subject @return [nil] if subject doesn't respond to given method
# File lib/utils/wallaby/module_utils.rb, line 13 def try_to(subject, method_id, *args, &block) return if method_id.blank? subject.respond_to?(method_id) && subject.public_send(method_id, *args, &block) || nil end
If block is given, run the block. Otherwise, return subject @param subject [Object] @yield [subject]
# File lib/utils/wallaby/module_utils.rb, line 41 def yield_for(subject) block_given? ? yield(subject) : subject end