Module: InheritanceHelper::Methods
- Defined in:
- lib/inheritance-helper/methods.rb
Overview
Collection of utility methods for rewriting class methods
Class Method Summary collapse
Instance Method Summary collapse
-
#add_value_to_class_method(method, value) ⇒ Object
useful for adding data to hashes.
-
#append_value_to_class_method(method, value) ⇒ Object
useful for working with arrays and maintain a list of values.
- #redefine_class_method(method, value) ⇒ Object
Class Method Details
.redefine_class_method(klass, method, value) ⇒ Object
6 7 8 9 |
# 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 |
Instance Method Details
#add_value_to_class_method(method, value) ⇒ Object
useful for adding data to hashes. when working with arrays it keeps a flat data set
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
useful for working with arrays and maintain a list of values
32 33 34 35 36 |
# 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) ⇒ Object
11 12 13 |
# File 'lib/inheritance-helper/methods.rb', line 11 def redefine_class_method(method, value) ::InheritanceHelper::Methods.redefine_class_method(self, method, value) end |