Class: Rails::GraphQL::Collectors::IdentedCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/graphql/collectors/idented_collector.rb

Overview

GraphQL Idented Collector

This collector helps building a indented string, most used for displaying a GraphQL schema

Instance Method Summary collapse

Constructor Details

#initialize(initial = 0, size = 2, auto_eol: true) ⇒ IdentedCollector

Returns a new instance of IdentedCollector.



9
10
11
12
13
# File 'lib/rails/graphql/collectors/idented_collector.rb', line 9

def initialize(initial = 0, size = 2, auto_eol: true)
  @size = size
  @val = [[initial, +'']]
  @auto_eol = auto_eol
end

Instance Method Details

#<<(str) ⇒ Object



44
45
46
47
# File 'lib/rails/graphql/collectors/idented_collector.rb', line 44

def <<(str)
  @val.last.last << str
  self
end

#blank?(pos = -1)) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rails/graphql/collectors/idented_collector.rb', line 70

def blank?(pos = -1)
  @val[pos].size.eql?(2) && @val[pos].last.empty?
end

#eolObject



49
50
51
52
# File 'lib/rails/graphql/collectors/idented_collector.rb', line 49

def eol
  @val.last << ''
  self
end

#indentObject



54
55
56
57
58
# File 'lib/rails/graphql/collectors/idented_collector.rb', line 54

def indent
  return @val.last[0] += @size if blank?
  @val << [last_ident + @size, +'']
  self
end

#indented(start = nil, finish = nil, auto_eol = @auto_eol) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rails/graphql/collectors/idented_collector.rb', line 15

def indented(start = nil, finish = nil, auto_eol = @auto_eol)
  self << start unless start.nil?

  indent
  yield

  @val.last.pop while @val.last.last.blank?
  unindent

  @val.pop(2) if blank?(-2)

  self << finish unless finish.nil?
  eol if auto_eol
  self
end

#last_identObject



66
67
68
# File 'lib/rails/graphql/collectors/idented_collector.rb', line 66

def last_ident
  @val.last.first
end

#puts(str) ⇒ Object



39
40
41
42
# File 'lib/rails/graphql/collectors/idented_collector.rb', line 39

def puts(str)
  @val.last.last << str
  eol
end

#unindentObject



60
61
62
63
64
# File 'lib/rails/graphql/collectors/idented_collector.rb', line 60

def unindent
  return @val.last[0] -= @size if blank?
  @val << [last_ident - @size, +'']
  self
end

#valueObject



31
32
33
34
35
36
37
# File 'lib/rails/graphql/collectors/idented_collector.rb', line 31

def value
  @val.map do |(ident, *content)|
    next if content.size.eql?(1) && content.first.blank?
    ident = (' ' * ident)
    ident + content.join("\n#{ident}")
  end.compact.join("\n")
end