Class: RFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rfile.rb

Overview

RFile provides a unified and easy way to check various attributes of a specific file or path. It provides simple access to various methods from the core File class.

It also offers several methods that provide simplified Find functionality.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ RFile

Returns a new instance of RFile.



36
37
38
# File 'lib/rfile.rb', line 36

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/rfile.rb', line 9

def path
  @path
end

Instance Method Details

#all_sub_nodes?(options = {}) ⇒ Boolean

Loops through all sub-directories and yields to a block for each node, passing in an RFile object for the current path.

Returns true if all yields returns true, otherwise returns false.

Useful for efficiently determining if any sub-directories or their files meet a user-defined criteria.

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/rfile.rb', line 74

def all_sub_nodes?(options={})
  sub_nodes(options) do |rf|
    return false unless yield(rf)
  end
  return true
end

#any_sub_node?(options = {}) ⇒ Boolean

Loops through all sub-directories and yields to a block for each node, passing in an RFile object for the current path.

Returns true if any yield returns true, otherwise returns false.

Useful for efficiently determining if any sub-directories or their files meet a user-defined criteria.

Returns:

  • (Boolean)


61
62
63
64
65
66
# File 'lib/rfile.rb', line 61

def any_sub_node?(options={})
  sub_nodes(options) do |rf|
    return true if yield(rf)
  end
  return false
end

#statObject

Lazy loading of the stat object.



29
30
31
32
33
34
# File 'lib/rfile.rb', line 29

def stat
  unless instance_variables.include?(:stat)
    @stat = File.stat(@path)
  end
  @stat
end

#sub_node_array(options = {}) ⇒ Object

Returns an array with an RFile object for every sub-directory and file below the current path.



83
84
85
86
87
88
89
# File 'lib/rfile.rb', line 83

def sub_node_array(options={})
  a = Array.new
  sub_nodes(options) do |rf|
    a << rf
  end
  return a
end

#sub_nodes(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/rfile.rb', line 44

def sub_nodes(options = {})
  opts = { :directories => true,
           :files => true }.merge(options)
  Find.find(@path) do |path|
     if ((File.file?(path) && opts[:files]) ||
         (File.directory?(path) && opts[:directories]))
       yield RFile.new(path)
     end
  end
end

#to_sObject



40
41
42
# File 'lib/rfile.rb', line 40

def to_s
  @path
end