Module: Interactor::Validation::CoreExt
- Included in:
- Module
- Defined in:
- lib/interactor/validation/core_ext.rb
Overview
Minimal core extensions - no external dependencies
Instance Method Summary collapse
-
#class_attribute(*names) ⇒ Object
Simple class attribute implementation with inheritance support.
Instance Method Details
#class_attribute(*names) ⇒ Object
Simple class attribute implementation with inheritance support
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/interactor/validation/core_ext.rb', line 8 def class_attribute(*names) names.each do |name| ivar_name = "@#{name}" # Class-level reader - checks own value, then parent define_singleton_method(name) do if instance_variable_defined?(ivar_name) instance_variable_get(ivar_name) elsif superclass.respond_to?(name) # When reading from parent, ensure we get our own copy first parent_value = superclass.public_send(name) # Deep copy parent value if it hasn't been set on this class yet if parent_value && !instance_variable_defined?(ivar_name) copied_value = deep_copy(parent_value) instance_variable_set(ivar_name, copied_value) copied_value else parent_value end end end # Class-level writer define_singleton_method("#{name}=") do |val| instance_variable_set(ivar_name, val) end # Instance-level reader delegates to class define_method(name) { self.class.public_send(name) } end end |