Class: GraphQLFormatter::LineOfCharacterStrings

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/graphql-formatter/line_of_character_strings.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent_level: 0) ⇒ LineOfCharacterStrings

Returns a new instance of LineOfCharacterStrings.



10
11
12
13
# File 'lib/graphql-formatter/line_of_character_strings.rb', line 10

def initialize(indent_level: 0)
  self.indent_level = indent_level
  @strings          = [GraphQLFormatter::CharacterString.new]
end

Instance Attribute Details

#indent_levelObject

Returns the value of attribute indent_level.



6
7
8
# File 'lib/graphql-formatter/line_of_character_strings.rb', line 6

def indent_level
  @indent_level
end

#stringsObject (readonly)

Returns the value of attribute strings.



5
6
7
# File 'lib/graphql-formatter/line_of_character_strings.rb', line 5

def strings
  @strings
end

Instance Method Details

#current_colorObject



39
40
41
# File 'lib/graphql-formatter/line_of_character_strings.rb', line 39

def current_color
  current_string.color
end

#current_stringObject



35
36
37
# File 'lib/graphql-formatter/line_of_character_strings.rb', line 35

def current_string
  @strings.last
end

#empty?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/graphql-formatter/line_of_character_strings.rb', line 43

def empty?
  strings.all? &:empty?
end

#last_charObject



26
27
28
29
# File 'lib/graphql-formatter/line_of_character_strings.rb', line 26

def last_char
  (current_string && current_string.last_char) ||
    (previous_string && previous_string.last_char)
end

#new_string(color: nil, **opts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/graphql-formatter/line_of_character_strings.rb', line 15

def new_string(color: nil, **opts)
  return current_string if color == current_color
  if current_string.empty?
    current_string.color = color
    return current_string
  end
  GraphQLFormatter::CharacterString.new(color: color, **opts).tap do |string|
    @strings << string
  end
end

#previous_stringObject



31
32
33
# File 'lib/graphql-formatter/line_of_character_strings.rb', line 31

def previous_string
  @strings[-2]
end

#to_s(indenter: (' ' * 4), **opts) ⇒ Object



47
48
49
50
# File 'lib/graphql-formatter/line_of_character_strings.rb', line 47

def to_s(indenter: (' ' * 4), **opts)
  indent = indenter * indent_level
  indent + reject(&:empty?).map { |str| str.to_s(**opts) }.join
end