Class: PackAPI::Pagination::PaginatorCursor::SqlLiteralSerializer

Inherits:
Object
  • Object
show all
Defined in:
lib/pack_api/pagination/paginator_cursor.rb

Overview

in order to recognize Arel::Nodes::SqlLiteral during parsing, we need to serialize it differently than a string

Class Method Summary collapse

Class Method Details

.deserialize(args) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/pack_api/pagination/paginator_cursor.rb', line 71

def self.deserialize(args)
  return args unless args.is_a?(Hash)
  return Arel.sql(args[:sql_literal][:raw_sql]) if args.key?(:sql_literal)

  args.to_h do |key, value|
    next [key, value] unless key.start_with?('sql_literal_')

    [Arel.sql(value[:raw_sql]), value[:hash_value]]
  end
end

.serialize(args) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/pack_api/pagination/paginator_cursor.rb', line 60

def self.serialize(args)
  return { sql_literal: { raw_sql: args.to_s } } if args.is_a?(Arel::Nodes::SqlLiteral)
  return args unless args.is_a?(Hash)

  args.map.with_index do |entry, index|
    next entry unless entry[0].is_a?(Arel::Nodes::SqlLiteral)

    ["sql_literal_#{index + 1}", { raw_sql: entry[0].to_s, hash_value: entry[1] }]
  end.to_h
end