Sha256: b29ec50550409ff3e7cb4c98a2fbdac0eb65155313654f526fb70197e90891bb

Contents?: true

Size: 957 Bytes

Versions: 2

Compression:

Stored size: 957 Bytes

Contents

module SpaceObject
  class Parser
    DELIMITER = /\n(?! )/
    LEAF = /^([^ ]+) /
    KEY_LINE = /^([^ ]+)(\n|$)/
    NEST_SPACING = /\n /

    def initialize(document)
      @document = (document || '')
    end

    def parse
      prepare_string
      parse_string(@document)
    end

    private
    def prepare_string
      @document =  @document.strip.gsub(/(\r\n|\n\r|\n\n+)/, "\n")
    end

    def populate_space(space, str)
    end

    def parse_string(str)
      str.split(DELIMITER).inject(Base.new) do |obj, space|
        key, value = if matches = KEY_LINE.match(space)
          offset = matches[1].length
          [matches[1], parse_string(space[offset..-1].gsub(NEST_SPACING, "\n"))]
        elsif matches = LEAF.match(space)
          offset = matches[1].length + 1
          [matches[1], space[offset..-1].gsub(NEST_SPACING, "\n")]
        end
        (key.nil? || key.empty?) or obj[key] = value
        obj
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
space_object-0.1.4 lib/space_object/parser.rb
space_object-0.1.3 lib/space_object/parser.rb