Module: Module::ChainMethod::ModuleMethods
- Defined in:
- lib/module/chain_method.rb
Instance Method Summary collapse
- #_chain_methods_suffix ⇒ Object
- #_map_selector(selector, suffix) ⇒ Object
- #chain_method(*selectors) ⇒ Object
- #chain_methods(suffix = nil) ⇒ Object
- #chain_methods!(target) ⇒ Object
- #included(target) ⇒ Object
-
#prepare_methods! ⇒ Object
NOT THREAD-SAFE!.
Instance Method Details
#_chain_methods_suffix ⇒ Object
31 32 33 34 |
# File 'lib/module/chain_method.rb', line 31 def _chain_methods_suffix @chain_methods_suffix ||= self.name.sub(/.*::/, '').freeze @_chain_methods_suffix ||= @chain_methods_suffix.to_s.sub(/[^a-z0-9_]/i, '_').freeze end |
#_map_selector(selector, suffix) ⇒ Object
88 89 90 91 92 |
# File 'lib/module/chain_method.rb', line 88 def _map_selector selector, suffix selector.to_s. sub(/\A([a-z0-9_]+)([^a-z0-9_]?)\Z/i) { | m | $1 + suffix + $2 }. to_sym end |
#chain_method(*selectors) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/module/chain_method.rb', line 36 def chain_method *selectors # $stderr.puts "ChainMethod::ModuleMethods #{self.inspect}.chain_method #{selectors.inspect}" chain_methods.push(*selectors.map{|m| m.to_sym}).uniq! # $stderr.puts "chain_selectors = #{chain_methods.inspect}" self end |
#chain_methods(suffix = nil) ⇒ Object
25 26 27 28 29 |
# File 'lib/module/chain_method.rb', line 25 def chain_methods suffix = nil # $stderr.puts "ChainMethod::ModuleMethods #{self.inspect}.chain_methods #{suffix.inspect}" @chain_methods_suffix = suffix if suffix @chain_methods ||= [ ] end |
#chain_methods!(target) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/module/chain_method.rb', line 70 def chain_methods! target # $stderr.puts "ChainMethod::ModuleMethods #{self.inspect}.chain_methods! #{target.inspect}" prepare_methods! suffix = _chain_methods_suffix # $stderr.puts " BEFORE: #{(target.instance_methods.sort - Object.methods).inspect}" chain_methods.each do | selector | without_selector = _map_selector(selector, "_without_#{suffix}") with_selector = _map_selector(selector, "_with_#{suffix}") # $stderr.puts " #{target.inspect}.alias #{without_selector.inspect} #{selector.inspect}" unless target.method_defined?(without_selector) target.send(:alias_method, without_selector, selector) end target.send(:alias_method, selector, with_selector) end # $stderr.puts " AFTER: #{(target.instance_methods.sort - Object.methods).inspect}" end |
#included(target) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/module/chain_method.rb', line 15 def included target # $stderr.puts "ChainMethod::ModuleMethods #{self.inspect}.included #{target}" super self.included_without_ChainMethod(target) if self.respond_to?(:included_without_ChainMethod) # $stderr.puts "#{target.inspect}.ancestors = #{target.ancestors.inspect}" target.extend(self.ClassMethods) if Class === target && defined?(self.ClassMethod) target.extend(self.ModuleMethods) if Module === target && defined?(self.ModuleMethods) chain_methods! target end |
#prepare_methods! ⇒ Object
NOT THREAD-SAFE!
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/module/chain_method.rb', line 44 def prepare_methods! @chain_methods_prepared ||= [ ] # $stderr.puts "ChainMethod::ModuleMethods #{self.inspect}.prepare_methods!" suffix = _chain_methods_suffix target = self # $stderr.puts " BEFORE: #{(target.instance_methods.sort - Object.methods).inspect}" chain_methods.each do | selector | next if @chain_methods_prepared.include?(selector) @chain_methods_prepared << selector without_selector = _map_selector(selector, "_without_#{suffix}") with_selector = _map_selector(selector, "_with_#{suffix}") # $stderr.puts " #{target.inspect}.alias #{without_selector.inspect} #{selector.inspect}" unless target.method_defined?(with_selector) target.send(:alias_method, with_selector, selector) end end # $stderr.puts " AFTER: #{(target.instance_methods.sort - Object.methods).inspect}" self end |