class SpaceObject::Parser

Constants

DELIMITER
KEY_LINE
LEAF
NEST_SPACING

Public Class Methods

new(document) click to toggle source
# File lib/space_object/parser.rb, line 8
def initialize(document)
  @document = (document || '')
end

Public Instance Methods

parse() click to toggle source
# File lib/space_object/parser.rb, line 12
def parse
  prepare_string
  parse_string(@document)
end

Private Instance Methods

parse_string(str) click to toggle source
# File lib/space_object/parser.rb, line 25
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
populate_space(space, str) click to toggle source
# File lib/space_object/parser.rb, line 22
def populate_space(space, str)
end
prepare_string() click to toggle source
# File lib/space_object/parser.rb, line 18
def prepare_string
  @document =  @document.strip.gsub(/(\r\n|\n\r|\n\n+)/, "\n")
end