Class: Graphiti::Util::AttributeCheck
- Defined in:
- lib/graphiti/util/attribute_check.rb
Instance Attribute Summary collapse
-
#flag ⇒ Object
readonly
Returns the value of attribute flag.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#raise_error ⇒ Object
readonly
Returns the value of attribute raise_error.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Class Method Summary collapse
Instance Method Summary collapse
- #attribute ⇒ Object
- #attribute? ⇒ Boolean
- #guard_passes? ⇒ Boolean
- #guarded? ⇒ Boolean
-
#initialize(resource, name, flag, request, raise_error) ⇒ AttributeCheck
constructor
A new instance of AttributeCheck.
- #maybe_raise(opts = {}) ⇒ Object
- #raise_error?(exists) ⇒ Boolean
- #request? ⇒ Boolean
- #run ⇒ Object
- #supported? ⇒ Boolean
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
#flag ⇒ Object (readonly)
Returns the value of attribute flag.
5 6 7 |
# File 'lib/graphiti/util/attribute_check.rb', line 5 def flag @flag end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/graphiti/util/attribute_check.rb', line 5 def name @name end |
#raise_error ⇒ Object (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 |
#request ⇒ Object (readonly)
Returns the value of attribute request.
5 6 7 |
# File 'lib/graphiti/util/attribute_check.rb', line 5 def request @request end |
#resource ⇒ Object (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
#attribute ⇒ Object
67 68 69 |
# File 'lib/graphiti/util/attribute_check.rb', line 67 def attribute @attribute ||= resource.all_attributes[name] end |
#attribute? ⇒ Boolean
71 72 73 |
# File 'lib/graphiti/util/attribute_check.rb', line 71 def attribute? !!attribute end |
#guard_passes? ⇒ Boolean
53 54 55 |
# File 'lib/graphiti/util/attribute_check.rb', line 53 def guard_passes? !!resource.send(attribute[flag]) end |
#guarded? ⇒ Boolean
57 58 59 60 61 |
# File 'lib/graphiti/util/attribute_check.rb', line 57 def guarded? request? && attribute[flag].is_a?(Symbol) && attribute[flag] != :required 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
75 76 77 78 79 80 81 |
# 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? ⇒ Boolean
83 84 85 |
# File 'lib/graphiti/util/attribute_check.rb', line 83 def request? !!request end |
#run ⇒ Object
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
63 64 65 |
# File 'lib/graphiti/util/attribute_check.rb', line 63 def supported? attribute[flag] != false end |