class Graphiti::Util::AttributeCheck

Attributes

flag[R]
name[R]
raise_error[R]
request[R]
resource[R]

Public Class Methods

new(resource, name, flag, request, raise_error) click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 11
def initialize(resource, name, flag, request, raise_error)
  @resource = resource
  @name = name.to_sym
  @flag = flag
  @request = request
  @raise_error = raise_error
end
run(resource, name, flag, request, raise_error) click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 7
def self.run(resource, name, flag, request, raise_error)
  new(resource, name, flag, request, raise_error).run
end

Public Instance Methods

attribute() click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 67
def attribute
  @attribute ||= resource.all_attributes[name]
end
attribute?() click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 71
def attribute?
  !!attribute
end
guard_passes?() click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 53
def guard_passes?
  !!resource.send(attribute[flag])
end
guarded?() click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 57
def guarded?
  request? &&
    attribute[flag].is_a?(Symbol) &&
    attribute[flag] != :required
end
maybe_raise(opts = {}) click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 39
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
raise_error?(exists) click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 75
def raise_error?(exists)
  if raise_error == :only_unsupported
    exists ? true : false
  else
    !!raise_error
  end
end
request?() click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 83
def request?
  !!request
end
run() click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 19
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
supported?() click to toggle source
# File lib/graphiti/util/attribute_check.rb, line 63
def supported?
  attribute[flag] != false
end