Class: Graphiti::Serializer
- Inherits:
-
JSONAPI::Serializable::Resource
- Object
- JSONAPI::Serializable::Resource
- Graphiti::Serializer
- Includes:
- Extensions::BooleanAttribute, Extensions::ExtraAttribute, SerializableHash, SerializableTempId
- Defined in:
- lib/graphiti/serializer.rb
Class Method Summary collapse
- .inherited(klass) ⇒ Object
-
.on_demand_links? ⇒ Boolean
Keyed on the sideloads hash itself, which is reassigned whenever a relationship is applied, so the answer is recomputed exactly then.
Instance Method Summary collapse
- #as_jsonapi(kwargs = {}) ⇒ Object
- #cursor ⇒ Object
-
#method_missing(id) ⇒ Object
Allow access to resource methods.
- #respond_to_missing?(method_name, include_private = true) ⇒ Boolean
Methods included from SerializableHash
#polymorphic_subclass?, #remote_resource?, #to_hash
Methods included from Extensions::ExtraAttribute
Methods included from Extensions::BooleanAttribute
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id) ⇒ Object
Allow access to resource methods
93 94 95 96 97 98 99 |
# File 'lib/graphiti/serializer.rb', line 93 def method_missing(id, ...) if @resource.respond_to?(id, true) @resource.send(id, ...) else super end end |
Class Method Details
.inherited(klass) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/graphiti/serializer.rb', line 37 def self.inherited(klass) super klass.class_eval do extend JSONAPI::Serializable::Resource::ConditionalFields # See #requested_relationships def self.relationship(name, = {}, &block) prev = Util::Hash.deep_dup(field_condition_blocks) super self.field_condition_blocks = prev # _register_condition mutates the hash, which ancestors share by # reference until reassigned - copy first so the guard doesn't leak. self.relationship_condition_blocks = Util::Hash.deep_dup(relationship_condition_blocks) _register_condition(relationship_condition_blocks, name, ) end # NB - avoid clobbering includes when sparse fieldset # https://github.com/jsonapi-rb/jsonapi-serializable/pull/102 # # We also override this method to ensure attributes and relationships # have separate condition blocks. This way an attribute and # relationship can have the same name, and the attribute can be # conditional without affecting the relationship. def requested_relationships(fields) @_relationships.select do |k, _| _conditionally_included?(self.class.relationship_condition_blocks, k) end end end end |
.on_demand_links? ⇒ Boolean
Keyed on the sideloads hash itself, which is reassigned whenever a relationship is applied, so the answer is recomputed exactly then.
29 30 31 32 33 34 35 |
# File 'lib/graphiti/serializer.rb', line 29 def self.on_demand_links? sideloads = relationship_sideloads return @on_demand_links.last if @on_demand_links&.first.equal?(sideloads) @on_demand_links = [sideloads, sideloads.each_value.any? { |sideload| sideload.link_mode == :on_demand }] @on_demand_links.last end |
Instance Method Details
#as_jsonapi(kwargs = {}) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/graphiti/serializer.rb', line 85 def as_jsonapi(kwargs = {}) super(**kwargs).tap do |hash| strip_relationships!(hash) add_links!(hash) end end |
#cursor ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/graphiti/serializer.rb', line 68 def cursor starting_offset = 0 page_param = @proxy.query.pagination if (page_number = page_param[:number]) page_size = page_param[:size] || @resource.page_default_size starting_offset = (page_number - 1) * page_size end if (cursor = page_param[:after]) starting_offset = cursor[:offset] end current_offset = @object.instance_variable_get(:@__graphiti_index) offset = starting_offset + current_offset + 1 # (+ 1 b/c o-base index) Base64.encode64({offset: offset}.to_json).chomp end |
#respond_to_missing?(method_name, include_private = true) ⇒ Boolean
101 102 103 |
# File 'lib/graphiti/serializer.rb', line 101 def respond_to_missing?(method_name, include_private = true) @resource.respond_to?(method_name, include_private) || super end |