Module: DestructureMagic

Included in:
Object, String, Symbol
Defined in:
lib/destructure/magic.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/destructure/magic.rb', line 4

def self.included(base)
  orig = base.instance_method(:=~)

  base.send(:define_method, :=~) do |pattern|
    if pattern.is_a?(Regexp)
      orig.bind(self).call(pattern)
    elsif pattern.is_a?(Proc)
      # stuff gets cranky if you try to factor this out
      caller_binding = binding.of_caller(1)
      caller_location = caller_locations(1,1)[0].label
      caller = caller_binding.eval('self')
      caller.class.send(:include, Destructure) unless caller.class.included_modules.include?(Destructure)
      caller.send(:dbind_internal, self, pattern.to_sexp(strip_enclosure: true, ignore_nested: true), caller_binding, caller_location)
    else
      super
    end
  end
end