Sha256: 7a0fb0c12fab916d965b23b85486eec960c95bb34991f8a5010c482113929d40

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

module Rivendell::Import
  class File

    attr_reader :name, :path

    def initialize(name, attributes = {})
      if attributes[:base_directory]
        @path = name
        @name = self.class.relative_filename(name, attributes[:base_directory])
      elsif attributes[:path]
        @name = name
        @path = attributes[:path]
      else
        @name = @path = name
      end
    end

    def self.relative_filename(path, base_directory)
      ::File.expand_path(path).gsub(%r{^#{::File.expand_path(base_directory)}/},"")
    end

    def to_s
      name
    end

    def basename
      ::File.basename(name, ".#{extension}")
    end

    def extension
      ::File.extname(name).gsub(/^\./,'')
    end

    def ==(other)
      other and path == other.path
    end

    def match(expression)
      name.match(expression).tap do |result|
        verb = result ? "match" : "doesn't match"
        Rivendell::Import.logger.debug "File #{verb} '#{expression}'"
        !!result
      end
    end

    def in(directory, &block)
      if match %r{^#{directory}/}
        yield 
      end
    end

    def exists?
      ::File.exists? path
    end

    def destroy!
      Rivendell::Import.logger.debug "Delete file #{path}"
      ::File.delete(path) if exists?
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rivendell-import-0.0.5 lib/rivendell/import/file.rb
rivendell-import-0.0.4 lib/rivendell/import/file.rb
rivendell-import-0.0.3 lib/rivendell/import/file.rb
rivendell-import-0.0.2 lib/rivendell/import/file.rb