Sha256: a7066f9a601da80767a25c7f2727fc0448ed517c294c61e46e9c021dc9d96be8

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module Maprename
  class ContentParser

    attr_accessor :config

    def initialize(config, file)
      @config = config
      @file = file
      @content = read_file_content(file)

      debug "  ContentParser: #@config"
    end

    def parse!(context)
      config[:fields].each do |field_definition|
        debug "      ContentParser field definition: #{field_definition}"
        fields = @content.match(Regexp.new(field_definition[:pattern])).to_a[1..-1]
        debug "      ContentParser fields: #{fields}"
        eval_var_definition(fields, field_definition, context)
      end
    end

    def eval_var_definition(fields, field_definition, context)
      value = field_definition[:value].gsub(/\$(\d+)/, fields[$1.to_i - 1])
      context.instance_eval "self.#{field_definition[:name]} = #{value.inspect}"
      debug "      ContentParser value: #{value}"
      debug "      ContentParser context: #{context}"
    end

    def read_file_content(file)
      encoding = config[:encoding] || 'UTF-8'
      content = IO.read(file, encoding: '%s:UTF-8' % encoding)
      if config[:pre_processor]
        IO.popen(config[:pre_processor]) do |pipe|
          pipe.write(content)
          pipe.close_write
          pipe.read
        end
      else
        content
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
maprename-0.1.5 lib/maprename/content_parser.rb