Sha256: 39f6cc74c284a9a05807d2372eb75279b4220438d1fb2fee4a88f8de61774084

Contents?: true

Size: 923 Bytes

Versions: 1

Compression:

Stored size: 923 Bytes

Contents

module Maprename
  class FileNameParser
    def initialize(config)
      @config = config
    end

    def parse!(subject, context)
      fields = if split_method?
        subject.split(Regexp.new(@config[:pattern]))
      else
        subject.match(Regexp.new(@config[:pattern])).to_a[1..-1]
      end

      @config[:fields].each do |field_definition|
        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}"

      if parse_config = field_definition[:name_parse]
        Maprename::FileNameParser.new(parse_config).parse!(context.instance_eval(field_definition[:name]), context)
      end
    end

    def split_method?
      @config[:method] == 'split'
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
maprename-0.1.0 lib/maprename/file_name_parser.rb