Module: GraphQL::Language::BlockString
- Defined in:
- lib/graphql/language/block_string.rb
Class Method Summary collapse
-
.break_line(line, length) {|first_parts.join| ... } ⇒ Object
: (String line, Integer length) { (String) -> void } -> void.
-
.contains_only_whitespace?(line) ⇒ Boolean
: (String line) -> bool.
-
.print(str, indent: '') ⇒ Object
: (String str, ?indent: String) -> String.
-
.trim_whitespace(str) ⇒ Object
Remove leading and trailing whitespace from a block string.
Class Method Details
.break_line(line, length) {|first_parts.join| ... } ⇒ Object
: (String line, Integer length) { (String) -> void } -> void
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/graphql/language/block_string.rb', line 94 def self.break_line(line, length, &block) return yield(line) if line.length < length + 5 parts = line.split(Regexp.new("((?: |^).{15,#{length - 40}}(?= |$))")) return yield(line) if parts.length < 4 # `parts` has at least 4 items: first_parts = parts.slice!(0, 3) #: as !nil yield(first_parts.join) parts.each_with_index do |part, i| next if i % 2 == 1 yield "#{part[1..-1]}#{parts[i + 1]}" end nil end |
.contains_only_whitespace?(line) ⇒ Boolean
: (String line) -> bool
113 114 115 |
# File 'lib/graphql/language/block_string.rb', line 113 def self.contains_only_whitespace?(line) line.match?(/^\s*$/) end |
.print(str, indent: '') ⇒ Object
: (String str, ?indent: String) -> String
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/graphql/language/block_string.rb', line 60 def self.print(str, indent: '') line_length = 120 - indent.length block_str = "".dup triple_quotes = "\"\"\"\n" block_str << indent block_str << triple_quotes if str.include?("\n") str.split("\n") do |line| if line == '' block_str << "\n" else break_line(line, line_length) do |subline| block_str << indent block_str << subline block_str << "\n" end end end else break_line(str, line_length) do |subline| block_str << indent block_str << subline block_str << "\n" end end block_str << indent block_str << triple_quotes end |
.trim_whitespace(str) ⇒ Object
Remove leading and trailing whitespace from a block string. See "Block Strings" in https://github.com/facebook/graphql/blob/master/spec/Section%202%20--%20Language.md : (String str) -> String
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/graphql/language/block_string.rb', line 10 def self.trim_whitespace(str) # Early return for the most common cases: if str == "" return "".dup elsif !(has_newline = str.include?("\n")) && !(str.start_with?(" ")) return str end lines = has_newline ? str.split("\n") : [str] common_indent = nil #: Integer? # find the common whitespace lines.each_with_index do |line, idx| if idx == 0 next end line_length = line.size leading = 0 while leading < line_length && line.getbyte(leading) == 32 leading += 1 end if leading < line_length && (common_indent.nil? || leading < common_indent) common_indent = leading end end # Remove the common whitespace if common_indent && common_indent > 0 lines.each_with_index do |line, idx| if idx == 0 next else line[0, common_indent] = "" end end end # Remove leading & trailing blank lines while (first_line = lines.first) && contains_only_whitespace?(first_line) lines.shift end while (last_line = lines.last) && contains_only_whitespace?(last_line) lines.pop end # Rebuild the string lines.size > 1 ? lines.join("\n") : (lines.first || "".dup) end |