Class: RFile
- Inherits:
-
Object
- Object
- RFile
- 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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#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.
-
#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.
-
#initialize(path) ⇒ RFile
constructor
A new instance of RFile.
-
#stat ⇒ Object
Lazy loading of the stat object.
-
#sub_node_array(options = {}) ⇒ Object
Returns an array with an RFile object for every sub-directory and file below the current path.
- #sub_nodes(options = {}) ⇒ Object
- #to_s ⇒ Object
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
#path ⇒ Object (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.
74 75 76 77 78 79 |
# File 'lib/rfile.rb', line 74 def all_sub_nodes?(={}) sub_nodes() 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.
61 62 63 64 65 66 |
# File 'lib/rfile.rb', line 61 def any_sub_node?(={}) sub_nodes() do |rf| return true if yield(rf) end return false end |
#stat ⇒ Object
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(={}) a = Array.new sub_nodes() 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( = {}) opts = { :directories => true, :files => true }.merge() Find.find(@path) do |path| if ((File.file?(path) && opts[:files]) || (File.directory?(path) && opts[:directories])) yield RFile.new(path) end end end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/rfile.rb', line 40 def to_s @path end |