Module: GosuWrapper::Util

Included in:
GosuWrapper
Defined in:
lib/gosu_wrapper/util.rb

Instance Method Summary collapse

Instance Method Details

#method_missing_for(regex, type:, &definition_blk) ⇒ Object



4
5
6
7
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
# File 'lib/gosu_wrapper/util.rb', line 4

def method_missing_for(regex, type:, &definition_blk)
  anon_module = Module.new do |mod|
    define_method(:method_missing) do |name, *args, **keywords, &caller_blk|
      match = name.to_s.scan(regex).flatten[0]
      if match
        if respond_to?(name)
          send(name, *args, &caller_blk)
        else
          if type == :instance
            instance_eval &(
              definition_blk.call(match, *args, **keywords, &caller_blk)
            )
          elsif type == :class
            singleton_class.class_exec &(
              definition_blk.call(match, *args, **keywords, &caller_blk)
            )
          end
        end
      else
        super(name, *args, **keywords, &caller_blk)
      end
    end
  end

  base = case type
  when :instance
    self
  when :class
    self.singleton_class
  end
  base.prepend anon_module

end