File: find_inherited_value.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (31 lines) | stat: -rw-r--r-- 854 bytes parent folder | download
1
2
3
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
# frozen_string_literal: true
module GraphQL
  class Schema
    module FindInheritedValue
      def self.extended(child_cls)
        child_cls.singleton_class.include(GraphQL::EmptyObjects)
      end

      def self.included(child_cls)
        child_cls.include(GraphQL::EmptyObjects)
      end

      private

      def find_inherited_value(method_name, default_value = nil)
        if self.is_a?(Class)
          superclass.respond_to?(method_name, true) ? superclass.send(method_name) : default_value
        else
          ancestors_except_self = ancestors
          ancestors_except_self.delete(self)
          ancestors_except_self.each do |ancestor|
            if ancestor.respond_to?(method_name, true)
              return ancestor.send(method_name)
            end
          end
          default_value
        end
      end
    end
  end
end