Sha256: 631baeccdbe4d76e5208db850fcd5b0236585742932a5671cdb977d681831a00

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module FactoryToys
  class Parser # Feature Factory
    attr_accessor :elements

    def elements
      @elements ||= {}
    end

    def initialize(data)
      rows = self.split_by_row(data)
      rows = self.process_elements(rows)
      elements[:base] = (rows.empty? ? '' : rows.join("\n") )
    end

    protected
    def process_elements(rows)
      row = 0
      while rows.size > row
        rows, row = self.process_element(rows, row)
      end
      return rows
    end

    def split_by_row(data)
      data.split("\n").map(&:strip).find_all{|str| !str.blank?}
    end

    def process_element(rows, row)
      if rows[row] =~ /^[\s]*([^\s]+)[\s]*=[\s]*<<-([^\s]+)[\s]*$/
        name, start_text = $1, $2
        rows = self.extract_element(rows, row, name, start_text)
        return rows, row
      else
        return rows, row + 1
      end
    end

    def extract_element(rows, row, name, start_text)
      end_row = find_row(rows, row, start_text)
      elements[name.to_sym] = rows[row..end_row].join("\n")
      (row == 0 ? [] : rows[0..row-1]) + rows[end_row+1..-1]
    end

    def find_row(rows, row, start_text)
      rows[row..-1].each_with_index do |row_text, i|
        return row + i if row_text.strip == start_text
      end
      raise FactoryToys::CloseTagNotFoundError
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
factory_toys-0.2.0 lib/factory_toys/parser.rb
factory_toys-0.1.1 lib/factory_toys/parser.rb