Sha256: 39b3d302f77206199a4c8bbeda26c65a6f30699cb8cd54b642378e74e86933b1

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

module Maprename
  class FileNameParser
    def initialize(config)
      @config = config
      debug "  FileNameParser: #@config"
    end

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

      debug "    FileNameParser fields: #{fields}"
      @config[:fields].each do |field_definition|
        eval_var_definition(fields, field_definition, context)
      end
    end

    def eval_var_definition(fields, field_definition, context)
      debug "      FileNameParser field definition: #{field_definition}"
      debug "      FileNameParser context: #{context}"

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

      debug "      FileNameParser value: #{value}"
      debug "      FileNameParser context: #{context}"
      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

5 entries across 5 versions & 1 rubygems

Version Path
maprename-0.1.5 lib/maprename/file_name_parser.rb
maprename-0.1.4 lib/maprename/file_name_parser.rb
maprename-0.1.3 lib/maprename/file_name_parser.rb
maprename-0.1.2 lib/maprename/file_name_parser.rb
maprename-0.1.1 lib/maprename/file_name_parser.rb