Sha256: 715682ae9b907c45626b30996ad25c31001d6df76709d30636c7a891c3f64c57

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 KB

Contents

# Author::    Eric Crane  (mailto:eric.crane@mac.com)
# Copyright:: Copyright (c) 2020 Eric Crane.  All rights reserved.
#
# Helper class used as part of file loading.
# It is responsible for splitting a line into components.
#

module Gloo
  module Persist
    class LineSplitter

      BEGIN_BLOCK = 'BEGIN'.freeze
      END_BLOCK = 'END'.freeze

      attr_reader :obj

      #
      # Set up a line splitter
      #
      def initialize( line, tabs )
        @line = line
        @tabs = tabs
      end

      #
      # Split the line into 3 parts.
      #
      def split
        detect_name
        detect_type
        detect_value

        return @name, @type, @value
      end

      #
      # Detect the object name.
      #
      def detect_name
        @line = @line.strip
        @idx = @line.index( ' ' )
        @idx = 0 unless @idx
        @name = @line[ 0..@idx - 1 ]
      end

      #
      # Detect the object type.
      #
      def detect_type
        @line = @line[ @idx + 1..-1 ]
        @idx = @line.index( ' ' )

        if @line[ 0 ] == ':'
          @type = 'untyped'
          return
        end

        @type = @line[ 0..( @idx ? @idx - 1 : -1 ) ]
        @type = @type[ 1..-1 ] if @type[ 0 ] == '['
        @type = @type[ 0..-2 ] if @type[ -1 ] == ']'
      end

      #
      # Detect the object value.
      # Use nil if there is no value specified.
      #
      def detect_value
        if @idx
          @value = @line[ @idx + 1..-1 ]
          if @value[ 0..1 ] == ': '
            @value = @value[ 2..-1 ]
          elsif @value[ 0 ] == ':'
            @value = @value[ 1..-1 ]
          end
        else
          @value = nil
        end
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gloo-3.10.1 lib/gloo/persist/line_splitter.rb
gloo-3.10.0 lib/gloo/persist/line_splitter.rb
gloo-3.9.1 lib/gloo/persist/line_splitter.rb
gloo-3.9.0 lib/gloo/persist/line_splitter.rb
gloo-3.8.0 lib/gloo/persist/line_splitter.rb
gloo-3.7.0 lib/gloo/persist/line_splitter.rb
gloo-3.6.2 lib/gloo/persist/line_splitter.rb
gloo-3.6.1 lib/gloo/persist/line_splitter.rb
gloo-3.6.0 lib/gloo/persist/line_splitter.rb