Class: Graphlyte::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/graphlyte/serializer.rb

Overview

Logic for writing a GraphQL document to a string

Defined Under Namespace

Modules: Refinements

Constant Summary collapse

Unsupported =
Class.new(ArgumentError)
SPACE =
' '
STEP =
2
NEWLINE =
"\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buff = []) ⇒ Serializer

Returns a new instance of Serializer.



132
133
134
135
136
137
# File 'lib/graphlyte/serializer.rb', line 132

def initialize(buff = [])
  @buff = buff
  @line_length = 100
  @max_fields_per_line = 5
  @indent = 0
end

Instance Attribute Details

#buffObject (readonly)

Returns the value of attribute buff.



129
130
131
# File 'lib/graphlyte/serializer.rb', line 129

def buff
  @buff
end

#indentObject (readonly)

Returns the value of attribute indent.



129
130
131
# File 'lib/graphlyte/serializer.rb', line 129

def indent
  @indent
end

#line_lengthObject

Returns the value of attribute line_length.



130
131
132
# File 'lib/graphlyte/serializer.rb', line 130

def line_length
  @line_length
end

#max_fields_per_lineObject

Returns the value of attribute max_fields_per_line.



130
131
132
# File 'lib/graphlyte/serializer.rb', line 130

def max_fields_per_line
  @max_fields_per_line
end

Instance Method Details

#<<(chunk) ⇒ Object



139
140
141
142
# File 'lib/graphlyte/serializer.rb', line 139

def <<(chunk)
  @buff << chunk.to_s
  self
end

#comma_separated(collection) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/graphlyte/serializer.rb', line 207

def comma_separated(collection)
  return unless collection
  return if collection.empty?

  buff << '('
  collection.each_with_index do |elem, i|
    buff << (', ' * [i, 1].min)
    elem.serialize(self)
  end
  buff << ')'
end

#dump_arguments(node) ⇒ Object



219
220
221
# File 'lib/graphlyte/serializer.rb', line 219

def dump_arguments(node)
  comma_separated(node.arguments)
end

#dump_definitions(definitions) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/graphlyte/serializer.rb', line 144

def dump_definitions(definitions)
  return unless definitions&.any?

  definitions.each_with_index do |dfn, i|
    buff << NEWLINE << NEWLINE if i.positive?
    dfn.serialize(self)
  rescue NoMethodError
    raise Unsupported, dfn.class
  end
end

#dump_directives(node) ⇒ Object



203
204
205
# File 'lib/graphlyte/serializer.rb', line 203

def dump_directives(node)
  node.directives&.each { _1.serialize(self) }
end

#dump_indented_selection(selection) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/graphlyte/serializer.rb', line 179

def dump_indented_selection(selection)
  selection.each do |selected|
    next_line

    selected.serialize(self)
  end

  buff << NEWLINE << (SPACE * (indent - STEP))
end

#dump_selection(node) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/graphlyte/serializer.rb', line 155

def dump_selection(node)
  i = @indent
  selection = node.selection
  return unless selection&.any?

  @indent += STEP

  buff << SPACE << '{'

  if simple?(selection)
    dump_simple_selection(selection)
  else
    dump_indented_selection(selection)
  end

  buff << '}'
ensure
  @indent = i
end

#dump_simple_selection(selection) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/graphlyte/serializer.rb', line 195

def dump_simple_selection(selection)
  buff << SPACE
  selection.each do |selected|
    selected.serialize(self)
    buff << SPACE
  end
end

#next_lineObject



175
176
177
# File 'lib/graphlyte/serializer.rb', line 175

def next_line
  buff << NEWLINE << (SPACE * indent)
end

#simple?(selection) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
# File 'lib/graphlyte/serializer.rb', line 189

def simple?(selection)
  selection.length < max_fields_per_line &&
    selection.all?(&:simple?) &&
    (selection.sum { _1.name.length } + selection.length + indent) < line_length
end