Sha256: 5f9d3513f9ae37c8e9fc152f6bbd7cf1070a7de509f01cf0498e4c7eea7f3603

Contents?: true

Size: 972 Bytes

Versions: 4

Compression:

Stored size: 972 Bytes

Contents

module AIRefactor
  class TemplatedPath
    def initialize(input_file, refactor_name, template)
      @input_file = input_file
      @refactor_name = refactor_name
      raise ArgumentError unless template.length.positive?
      @template = template
    end

    def generate
      path_from_template
    end

    private

    def path_from_template
      path = @template.dup
      @template.scan(/\[(FILE|NAME|DIR|REFACTOR|EXT)(\|([^|]+)\|([^\]]*))?\]/).each do |match|
        type, sub, old_value, new_value = match
        value = send(type.downcase.to_sym)
        value = value.gsub(old_value, new_value) if sub
        path.gsub!("[#{type}#{sub}]", value)
      end
      path
    end

    def file
      File.basename(@input_file)
    end

    def name
      File.basename(@input_file, ".*")
    end

    def dir
      File.dirname(@input_file)
    end

    def refactor
      @refactor_name
    end

    def ext
      File.extname(@input_file)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ai_refactor-0.6.0 lib/ai_refactor/templated_path.rb
ai_refactor-0.5.4 lib/ai_refactor/templated_path.rb
ai_refactor-0.5.3 lib/ai_refactor/templated_path.rb
ai_refactor-0.5.2 lib/ai_refactor/templated_path.rb