Method: RefinementBuilder.build_refinement

Defined in:
lib/refinement_builder.rb

.build_refinement(class_name, namespace: Object, refines: Object, &blk) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/refinement_builder.rb', line 3

def build_refinement(class_name, namespace: Object, refines: Object, &blk)
  const = namespace.const_set class_name, Module.new(&blk)
  const.instance_methods(false).each do |method_name|
    orig_method = const.instance_method method_name
    const.send :define_method, method_name do |*args, **keywords, &blk|
      if eql?(const)
        if keywords.empty?
          orig_method.bind(const).call *args, &blk
        else
          orig_method.bind(const).call *args, **keywords, &blk
        end
      else
        if keywords.empty?
          const.send method_name, *args, &blk
        else
          const.send method_name, *args, **keywords, &blk
        end
      end
    end
    const.send :module_function, method_name
  end
  const.send(:refine, refines) { include const }
end