Sha256: 456ad01b2681dbee193e3b51faff8be30dcd5c8f60f8e288fc6be7ab9834fb63

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 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")
    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.2 lib/factory_toys/parser.rb
factory_toys-0.2.1 lib/factory_toys/parser.rb