Class: Rumai::Node
Overview
An entry in the IXP file system.
Direct Known Subclasses
Constant Summary collapse
- @@cache =
Hash.new {|h,k| h[k] = Node.new(k) }
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#[](sub_path) ⇒ Object
Returns the given sub-path as a Node object.
-
#children ⇒ Object
Returns all child nodes of this node.
-
#clear ⇒ Object
Deletes all child nodes.
-
#create(*args) ⇒ Object
Creates a file corresponding to this node on the IXP server.
-
#directory? ⇒ Boolean
Tests if this node is a directory.
-
#each(&block) ⇒ Object
Iterates through each child of this directory.
-
#each_line {|line| ... } ⇒ Object
Invokes the given block for every line in the content of this node.
-
#entries ⇒ Object
Returns the names of all files in this directory.
-
#exist? ⇒ Boolean
Tests if this node exists on the IXP server.
-
#initialize(path) ⇒ Node
constructor
A new instance of Node.
-
#method_missing(meth, *args) ⇒ Object
Provides access to child nodes through method calls.
-
#open(mode = 'r', &block) ⇒ Object
Opens this node for I/O access.
-
#parent ⇒ Object
Returns the parent node of this node.
-
#read(*args) ⇒ Object
Returns the entire content of this node.
-
#remove ⇒ Object
Deletes the file corresponding to this node on the IXP server.
-
#stat ⇒ Object
Returns file statistics about this node.
-
#write(content) ⇒ Object
Writes the given content to this node.
Methods included from ExportInstanceMethods
Constructor Details
#initialize(path) ⇒ Node
Returns a new instance of Node.
32 33 34 |
# File 'lib/rumai/fs.rb', line 32 def initialize path @path = path.to_s.squeeze('/') end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
Provides access to child nodes through method calls.
:call-seq: node.child -> Node
183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/rumai/fs.rb', line 183 def method_missing meth, *args child = self[meth] # speed up future accesses (class << self; self; end).instance_eval do define_method meth do child end end child end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
30 31 32 |
# File 'lib/rumai/fs.rb', line 30 def path @path end |
Instance Method Details
#[](sub_path) ⇒ Object
Returns the given sub-path as a Node object.
142 143 144 |
# File 'lib/rumai/fs.rb', line 142 def [] sub_path @@cache[ File.join(@path, sub_path.to_s) ] end |
#children ⇒ Object
Returns all child nodes of this node.
156 157 158 |
# File 'lib/rumai/fs.rb', line 156 def children entries.map! {|c| self[c] } end |
#clear ⇒ Object
Deletes all child nodes.
172 173 174 175 176 |
# File 'lib/rumai/fs.rb', line 172 def clear children.each do |c| c.remove end end |
#create(*args) ⇒ Object
Creates a file corresponding to this node on the IXP server.
124 125 126 |
# File 'lib/rumai/fs.rb', line 124 def create *args IXP_AGENT.create @path, *args end |
#directory? ⇒ Boolean
Tests if this node is a directory.
61 62 63 |
# File 'lib/rumai/fs.rb', line 61 def directory? exist? and stat.directory? end |
#each(&block) ⇒ Object
Iterates through each child of this directory.
165 166 167 |
# File 'lib/rumai/fs.rb', line 165 def each &block children.each(&block) end |
#each_line {|line| ... } ⇒ Object
Invokes the given block for every line in the content of this node.
101 102 103 104 105 106 107 108 |
# File 'lib/rumai/fs.rb', line 101 def each_line &block raise ArgumentError unless block_given? open do |file| until (chunk = file.read(true)).empty? chunk.each_line(&block) end end end |
#entries ⇒ Object
Returns the names of all files in this directory.
70 71 72 73 74 75 76 |
# File 'lib/rumai/fs.rb', line 70 def entries begin IXP_AGENT.entries @path rescue IXP::Error [] end end |
#exist? ⇒ Boolean
Tests if this node exists on the IXP server.
48 49 50 51 52 53 54 |
# File 'lib/rumai/fs.rb', line 48 def exist? begin true if stat rescue IXP::Error false end end |
#open(mode = 'r', &block) ⇒ Object
Opens this node for I/O access.
83 84 85 |
# File 'lib/rumai/fs.rb', line 83 def open mode = 'r', &block IXP_AGENT.open @path, mode, &block end |
#parent ⇒ Object
Returns the parent node of this node.
149 150 151 |
# File 'lib/rumai/fs.rb', line 149 def parent @@cache[ File.dirname(@path) ] end |
#read(*args) ⇒ Object
Returns the entire content of this node.
92 93 94 |
# File 'lib/rumai/fs.rb', line 92 def read *args IXP_AGENT.read @path, *args end |
#remove ⇒ Object
Deletes the file corresponding to this node on the IXP server.
133 134 135 |
# File 'lib/rumai/fs.rb', line 133 def remove IXP_AGENT.remove @path end |
#stat ⇒ Object
Returns file statistics about this node.
41 42 43 |
# File 'lib/rumai/fs.rb', line 41 def stat IXP_AGENT.stat @path end |
#write(content) ⇒ Object
Writes the given content to this node.
115 116 117 |
# File 'lib/rumai/fs.rb', line 115 def write content IXP_AGENT.write @path, content end |