Sha256: 37ee735c094309fb387bcf4782e7df8b487db2b9ad0b442432d732ed68c4e745

Contents?: true

Size: 1.95 KB

Versions: 43

Compression:

Stored size: 1.95 KB

Contents

# Private, tested in resource specs
module Graphiti
  module Util
    class AttributeCheck
      attr_reader :resource, :name, :flag, :request, :raise_error

      def self.run(resource, name, flag, request, raise_error)
        new(resource, name, flag, request, raise_error).run
      end

      def initialize(resource, name, flag, request, raise_error)
        @resource = resource
        @name = name.to_sym
        @flag = flag
        @request = request
        @raise_error = raise_error
      end

      def run
        if attribute?
          if supported?
            if guarded?
              if guard_passes?
                attribute
              else
                maybe_raise(request: true, guard: attribute[flag])
              end
            else
              attribute
            end
          else
            maybe_raise(exists: true)
          end
        else
          maybe_raise(exists: false)
        end
      end

      def maybe_raise(opts = {})
        default = {request: request, exists: true}
        opts = default.merge(opts)
        error_class = opts[:exists] ?
          Graphiti::Errors::InvalidAttributeAccess :
          Graphiti::Errors::UnknownAttribute

        if raise_error?(opts[:exists])
          raise error_class.new(resource, name, flag, **opts)
        else
          false
        end
      end

      def guard_passes?
        !!resource.send(attribute[flag])
      end

      def guarded?
        request? &&
          attribute[flag].is_a?(Symbol) &&
          attribute[flag] != :required
      end

      def supported?
        attribute[flag] != false
      end

      def attribute
        @attribute ||= resource.all_attributes[name]
      end

      def attribute?
        !!attribute
      end

      def raise_error?(exists)
        if raise_error == :only_unsupported
          exists ? true : false
        else
          !!raise_error
        end
      end

      def request?
        !!request
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
graphiti-1.7.6 lib/graphiti/util/attribute_check.rb
graphiti-1.7.5 lib/graphiti/util/attribute_check.rb
graphiti-1.7.4 lib/graphiti/util/attribute_check.rb
graphiti-1.7.3 lib/graphiti/util/attribute_check.rb
graphiti-1.7.2 lib/graphiti/util/attribute_check.rb
graphiti-1.7.1 lib/graphiti/util/attribute_check.rb
graphiti-1.7.0 lib/graphiti/util/attribute_check.rb
graphiti-1.6.4 lib/graphiti/util/attribute_check.rb
graphiti-1.6.3 lib/graphiti/util/attribute_check.rb
graphiti-1.6.2 lib/graphiti/util/attribute_check.rb
graphiti-1.6.1 lib/graphiti/util/attribute_check.rb
graphiti-1.6.0 lib/graphiti/util/attribute_check.rb
graphiti-1.5.3 lib/graphiti/util/attribute_check.rb
graphiti-1.5.2 lib/graphiti/util/attribute_check.rb
graphiti-1.5.1 lib/graphiti/util/attribute_check.rb
graphiti-1.5.0 lib/graphiti/util/attribute_check.rb
graphiti-1.4.0 lib/graphiti/util/attribute_check.rb
graphiti-1.3.9 lib/graphiti/util/attribute_check.rb
graphiti-1.3.8 lib/graphiti/util/attribute_check.rb
graphiti-1.3.7 lib/graphiti/util/attribute_check.rb