Sha256: bf33f2d28a73187cdcc49a402e992109ef60ff9d04ec3dfbe4319a034b8a6df7

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

require 'etc'

module Ru
  class File
    def initialize(path)
      @path = path
    end

    def basename
      delegate_to_file(:basename)
    end

    def ctime
      delegate_to_file(:ctime)
    end

    def extname
      delegate_to_file(:extname)
    end

    def format(format='l')
      separator = "\t"
      case format
      when 'l'
        datetime = ctime.strftime(%w{%Y-%m-%d %H:%M}.join(separator))
        return [omode, owner, group, size, datetime, name].join(separator)
      else
        raise 'format not supported'
      end
    end

    def ftype
      delegate_to_file(:ftype)
    end

    def gid
      delegate_to_stat(:gid)
    end

    def group
      grgid = Etc.getgrgid(gid)
      if grgid.nil?       # running on Windows
        ''
      else
        grgid.name
      end
    end

    def mode
      delegate_to_stat(:mode)
    end

    def mtime
      delegate_to_file(:mtime)
    end

    def omode
      '%o' % world_readable?
    end

    def owner
      pwuid = Etc.getpwuid(uid)
      if pwuid.nil?       # running on Windows
        ''
      else
        pwuid.name
      end
    end

    def size
      delegate_to_file(:size)
    end

    def to_s
      name
    end

    def uid
      delegate_to_stat(:uid)
    end

    def world_readable?
      delegate_to_stat(:world_readable?)
    end

    def <=>(other)
      name <=> other.name
    end

    alias_method :name, :basename
    alias_method :created_at, :ctime
    alias_method :updated_at, :mtime

    private

    def delegate_to_file(method)
      ::File.send(method, @path)
    end

    def delegate_to_stat(method)
      ::File.stat(@path).send(method)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ru2-2.6.0 lib/ru/file.rb
ru2-2.5.0 lib/ru/file.rb
ru2-2.4.0 lib/ru/file.rb
ru2-2.3.1 lib/ru/file.rb
ru2-2.3.0 lib/ru/file.rb
ru2-2.2.0 lib/ru/file.rb
ru2-2.1.5 lib/ru/file.rb