Class: Graphiti::Util::AttributeCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti/util/attribute_check.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, name, flag, request, raise_error) ⇒ AttributeCheck

Returns a new instance of AttributeCheck.



11
12
13
14
15
16
17
# 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

Instance Attribute Details

#flagObject (readonly)

Returns the value of attribute flag.



5
6
7
# File 'lib/graphiti/util/attribute_check.rb', line 5

def flag
  @flag
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/graphiti/util/attribute_check.rb', line 5

def name
  @name
end

#raise_errorObject (readonly)

Returns the value of attribute raise_error.



5
6
7
# File 'lib/graphiti/util/attribute_check.rb', line 5

def raise_error
  @raise_error
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/graphiti/util/attribute_check.rb', line 5

def request
  @request
end

#resourceObject (readonly)

Returns the value of attribute resource.



5
6
7
# File 'lib/graphiti/util/attribute_check.rb', line 5

def resource
  @resource
end

Class Method Details

.run(resource, name, flag, request, raise_error) ⇒ Object



7
8
9
# 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

Instance Method Details

#attributeObject



94
95
96
# File 'lib/graphiti/util/attribute_check.rb', line 94

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

#attribute?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/graphiti/util/attribute_check.rb', line 98

def attribute?
  !!attribute
end

#call_guard(*args) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/graphiti/util/attribute_check.rb', line 64

def call_guard(*args)
  if guard.is_a?(Proc)
    resource.instance_exec(*args, &guard)
  else
    resource.send(guard, *args)
  end
end

#guardObject



79
80
81
# File 'lib/graphiti/util/attribute_check.rb', line 79

def guard
  attribute[flag]
end

#guard_arityObject



72
73
74
75
76
77
# File 'lib/graphiti/util/attribute_check.rb', line 72

def guard_arity
  method = guard.is_a?(Proc) ? guard : resource.method(guard)
  # Negative arity means optional or splatted params - hand over
  # everything and let the guard take what it wants.
  method.arity.negative? ? 2 : method.arity
end

#guard_passes?Boolean

Guards may optionally accept the model being written and the attribute name, mirroring readable guards.

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
# File 'lib/graphiti/util/attribute_check.rb', line 55

def guard_passes?
  result = case guard_arity
  when 0 then call_guard
  when 1 then call_guard(resource.guard_model)
  else call_guard(resource.guard_model, name.to_s)
  end
  !!result
end

#guarded?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/graphiti/util/attribute_check.rb', line 83

def guarded?
  return false unless request?
  return false if guard == :required

  guard.is_a?(Symbol) || guard.is_a?(String) || guard.is_a?(Proc)
end

#maybe_raise(opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
# File 'lib/graphiti/util/attribute_check.rb', line 102

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

#request?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/graphiti/util/attribute_check.rb', line 110

def request?
  !!request
end

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/graphiti/util/attribute_check.rb', line 90

def supported?
  attribute[flag] != false
end