Sha256: d0da0b1e4fecbe4c1e57c257502e354d7a75d15b47b1c6774110d900be18fd81

Contents?: true

Size: 1.6 KB

Versions: 6

Compression:

Stored size: 1.6 KB

Contents

module Rails # :nodoc:
  module GraphQL # :nodoc:
    module Collectors # :nodoc:
      # This collector helps building a indented string
      class IdentedCollector
        def initialize(initial = 0, size = 2, auto_eol: true)
          @size = size
          @val = [[initial, '']]
          @auto_eol = auto_eol
        end

        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

        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

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

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

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

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

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

        def last_ident
          @val.last.first
        end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails-graphql-0.2.1 lib/rails/graphql/collectors/idented_collector.rb
rails-graphql-0.2.0 lib/rails/graphql/collectors/idented_collector.rb
rails-graphql-0.1.3 lib/rails/graphql/collectors/idented_collector.rb
rails-graphql-0.1.2 lib/rails/graphql/collectors/idented_collector.rb
rails-graphql-0.1.1 lib/rails/graphql/collectors/idented_collector.rb
rails-graphql-0.1.0 lib/rails/graphql/collectors/idented_collector.rb