Class: JSONAPIonify::Api::Resource::Builders::CursorBuilder

Inherits:
BaseBuilder
  • Object
show all
Defined in:
lib/jsonapionify/api/resource/builders/cursor_builder.rb

Constant Summary

Constants included from Structure

Structure::ValidationError

Instance Attribute Summary collapse

Attributes inherited from BaseBuilder

#resource

Instance Method Summary collapse

Methods inherited from BaseBuilder

build

Constructor Details

#initialize(resource, instance:, context:) ⇒ CursorBuilder

Returns a new instance of CursorBuilder.



10
11
12
13
14
# File 'lib/jsonapionify/api/resource/builders/cursor_builder.rb', line 10

def initialize(resource, instance:, context:)
  super(resource)
  @instance = instance
  @context  = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/jsonapionify/api/resource/builders/cursor_builder.rb', line 5

def context
  @context
end

#instanceObject (readonly)

Returns the value of attribute instance.



5
6
7
# File 'lib/jsonapionify/api/resource/builders/cursor_builder.rb', line 5

def instance
  @instance
end

Instance Method Details

#buildObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jsonapionify/api/resource/builders/cursor_builder.rb', line 16

def build
  sort_string        = params['sort']
  sort_fields        = sort_fields_from_sort_string(sort_string)
  attrs_with_values  = sort_fields.each_with_object({}) do |field, hash|
    hash[field.name] = instance.send(field.name)
  end
  Base64.urlsafe_encode64(JSON.dump(
    {
      t: resource_type,
      s: sort_string,
      a: attrs_with_values
    }
  ))
end