Module: DeepCover::Node::Mixin::HasChildHandler::ClassMethods

Defined in:
lib/deep_cover/node/mixin/has_child_handler.rb

Instance Method Summary collapse

Instance Method Details

#has_child_handler(template) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/deep_cover/node/mixin/has_child_handler.rb', line 26

def has_child_handler(template)
  child_method_name = format(template, name: 'child')
  action = template.gsub(/_%{name}/, '').gsub(/%{name}_/, '')
  const_name = "#{Tools.camelize(action)}Handler"
  class_eval <<-EVAL, __FILE__, __LINE__ + 1
    module #{const_name}                                     # module RewriteHandler
      module ClassMethods                                    #   module ClassMethods
        def has_child(#{action}: nil, **args)                #     def has_child(rewrite: nil, **args)
          name, _types = args.first                          #       name, _types = args.first
          define_child_handler(#{template.inspect},          #       define_child_handler('rewrite_%{child}',
            name, #{action})                                 #         name, rewrite)
          super(**args)                                      #       super(**args)
        end                                                  #     end
      end                                                    #   end

      def #{child_method_name}(child, name = nil)            #   def rewrite_child(child, name = nil)
        call_child_handler(#{template.inspect}, child,       #     call_child_handler('rewrite_%{child}', child,
          name) || super                                     #       name) || super
      end                                                    #   end
    end                                                      # end
    include #{const_name}                                    # include RewriteHandler
    singleton_class.prepend #{const_name}::ClassMethods      # singleton_class.prepend RewriteHandler::ClassMethods
  EVAL
end