Module: ModuleShims::Switch
- Defined in:
- lib/module_shims/switch.rb
Instance Method Summary collapse
- #default_target ⇒ Object
- #disable_shim ⇒ Object
- #enable_shim(shadow_target = true) ⇒ Object
- #insert_shim ⇒ Object
- #shim_const ⇒ Object
- #target_const ⇒ Object
- #target_const=(target = default_target) ⇒ Object
Instance Method Details
#default_target ⇒ Object
44 45 46 |
# File 'lib/module_shims/switch.rb', line 44 def default_target const_get(name.gsub(/\A[^:]*/, '')) end |
#disable_shim ⇒ Object
9 10 11 12 |
# File 'lib/module_shims/switch.rb', line 9 def disable_shim target_const.prefix_existing_instance_methods('__module_switch', self, true) target_const.remove_instance_methods_from_ancestors("#{name}::SHIM") end |
#enable_shim(shadow_target = true) ⇒ Object
3 4 5 6 7 |
# File 'lib/module_shims/switch.rb', line 3 def enable_shim(shadow_target = true) insert_shim unless @inserted target_const.prefix_existing_instance_methods('__module_switch', self) if shadow_target shim_const.copy_existing_instance_methods(self) end |
#insert_shim ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/module_shims/switch.rb', line 14 def insert_shim @inserted ||= begin target_const.send(:prepend, shim_const) true end end |
#shim_const ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/module_shims/switch.rb', line 22 def shim_const return const_get('SHIM') if const_defined?('SHIM') # use predefined_method to look up SHIM module. It's to support "reload!" in rails console predefined_method = "#{self.name.downcase.gsub(/::/, '_')}_shim" existing_shim = target_const.ancestors.find { |anc| anc.respond_to? predefined_method } unless existing_shim existing_shim = Module.new existing_shim.singleton_class.send(:define_method, predefined_method) {} end const_set('SHIM', existing_shim) end |
#target_const ⇒ Object
36 37 38 |
# File 'lib/module_shims/switch.rb', line 36 def target_const @target ||= default_target end |
#target_const=(target = default_target) ⇒ Object
40 41 42 |
# File 'lib/module_shims/switch.rb', line 40 def target_const=(target = default_target) @target = target end |