Module: WC::Essentials::ClassHelpers
- Defined in:
- lib/wc/essentials/class_helpers.rb
Overview
Helpers for classes
Class Method Summary collapse
-
.assign_class_variable(klass, name, value) ⇒ Object
Assign class variable and also merge parent class variable.
-
.concat_var(klass, name, value) ⇒ Object
Concat values from value provided with value assigned to the class variable.
-
.klass_var_name(klass, name) ⇒ Object
Get the class variable name.
-
.var_value(klass, name, default = []) ⇒ Object
Return the value of class variable.
Class Method Details
.assign_class_variable(klass, name, value) ⇒ Object
Assign class variable and also merge parent class variable. Usefull if required to have class variable only bind to the class in the case of inheritence
26 27 28 29 30 31 32 33 |
# File 'lib/wc/essentials/class_helpers.rb', line 26 def self.assign_class_variable(klass, name, value) klass.ancestors.each do |ancestor| value = concat_var(ancestor, name, value) end k_var_name = klass_var_name(klass, name) klass.class_variable_set(k_var_name, value) end |
.concat_var(klass, name, value) ⇒ Object
Concat values from value provided with value assigned to the class variable
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/wc/essentials/class_helpers.rb', line 41 def self.concat_var(klass, name, value) var_name = klass_var_name(klass, name) existing = var_value( klass, var_name, value.is_a?(Array) ? [] : {} ) (value.is_a?(Array) ? (existing + value) : value.merge(existing)) end |
.klass_var_name(klass, name) ⇒ Object
Get the class variable name
11 12 13 14 15 16 17 |
# File 'lib/wc/essentials/class_helpers.rb', line 11 def self.klass_var_name(klass, name) "@@#{ klass.name.split('::') .join('_') .downcase }_#{name}" end |
.var_value(klass, name, default = []) ⇒ Object
Return the value of class variable
58 59 60 61 62 63 64 |
# File 'lib/wc/essentials/class_helpers.rb', line 58 def self.var_value(klass, name, default = []) return default unless defined?(klass.class_variable_get(name)) klass.class_variable_get(name) rescue StandardError default end |