Sha256: 898a49e671744e599cbf57d240a178680840029c54fa19ae0b3eac8383417d13

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 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: 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.1 lib/maprename/content_parser.rb