Sha256: 9b9fce28e260e6fd56300bdfc6917f03d4116bb75575020ba45455c354f6d362

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Rails
  module GraphQL
    module Helpers
      # Helper focus on event execution when objects have a owner property,
      # pratically allowing resolvers and similar to be called in the owner
      module WithOwner
        def self.included(other)
          other.extend(WithOwner::ClassMethods)
          other.class_attribute(:owner, instance_writer: false)
        end

        module ClassMethods
          def method_defined?(method_name)
            super || owner&.method_defined?(method_name)
          end
        end

        private

          def respond_to_missing?(*args)
            owner_respond_to?(*args) || super
          end

          def method_missing(method_name, *args, **xargs, &block)
            return super unless owner_respond_to?(method_name)
            event.on_instance(owner) do |obj|
              obj.public_send(method_name, *args, **xargs, &block)
            end
          end

          # Since owners are classes, this checks for the instance methods of
          # it, since this is a instance method
          def owner_respond_to?(method_name, with_private = false)
            return true if !owner.is_a?(Class) && owner.respond_to?(method_name, with_private)
            (with_private ? %i[public protected private] : %i[public]).any? do |type|
              owner.send("#{type}_instance_methods").include?(method_name)
            end unless owner.nil?
          end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails-graphql-1.0.2 lib/rails/graphql/helpers/with_owner.rb
rails-graphql-1.0.1 lib/rails/graphql/helpers/with_owner.rb
rails-graphql-1.0.0 lib/rails/graphql/helpers/with_owner.rb
rails-graphql-1.0.0.rc2 lib/rails/graphql/helpers/with_owner.rb
rails-graphql-1.0.0.rc1 lib/rails/graphql/helpers/with_owner.rb
rails-graphql-1.0.0.beta lib/rails/graphql/helpers/with_owner.rb