module InheritanceHelper::Methods

Collection of utility methods for rewriting class methods

Public Class Methods

redefine_class_method(klass, method, value) click to toggle source
# File lib/inheritance-helper/methods.rb, line 6
def self.redefine_class_method(klass, method, value)
  class << klass; self; end.send(:define_method, method) { value }
  klass
end

Public Instance Methods

add_value_to_class_method(method, value) click to toggle source

useful for adding data to hashes. when working with arrays it keeps a flat data set

# File lib/inheritance-helper/methods.rb, line 17
def add_value_to_class_method(method, value)
  old_value = send(method)

  new_value =
    case old_value
    when Hash
      old_value.merge(value)
    else
      old_value + Array(value)
    end

  redefine_class_method(method, old_value.frozen? ? new_value.freeze : new_value)
end
append_value_to_class_method(method, value) click to toggle source

useful for working with arrays and maintain a list of values

# File lib/inheritance-helper/methods.rb, line 32
def append_value_to_class_method(method, value)
  old_value = send(method)
  new_value = old_value.dup << value
  redefine_class_method(method, old_value.frozen? ? new_value.freeze : new_value)
end
redefine_class_method(method, value) click to toggle source
# File lib/inheritance-helper/methods.rb, line 11
def redefine_class_method(method, value)
  ::InheritanceHelper::Methods.redefine_class_method(self, method, value)
end