Class: Rumai::Node

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/rumai/fs.rb

Overview

An entry in the IXP file system.

Direct Known Subclasses

WidgetNode

Constant Summary

@@cache =
Hash.new {|h,k| h[k] = Node.new(k) }

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (Node) initialize(path)

A new instance of Node



35
36
37
# File 'lib/rumai/fs.rb', line 35

def initialize path
  @path = path.to_s.squeeze('/')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(meth, *args)

Provides access to child nodes through method calls.

:call-seq: node.child -> Node



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/rumai/fs.rb', line 175

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

- (Object) path (readonly)

Returns the value of attribute path



33
34
35
# File 'lib/rumai/fs.rb', line 33

def path
  @path
end

Instance Method Details

- (Object) [](sub_path)

Returns the given sub-path as a Node object.



134
135
136
# File 'lib/rumai/fs.rb', line 134

def [] sub_path
  @@cache[ File.join(@path, sub_path.to_s) ]
end

- (Object) children

Returns all child nodes of this node.



148
149
150
# File 'lib/rumai/fs.rb', line 148

def children
  entries.map! {|c| self[c] }
end

- (Object) clear

Deletes all child nodes.



164
165
166
167
168
# File 'lib/rumai/fs.rb', line 164

def clear
  children.each do |c|
    c.remove
  end
end

- (Object) create(*args)

Creates a file corresponding to this node on the IXP server.



120
121
122
# File 'lib/rumai/fs.rb', line 120

def create *args
  IXP_AGENT.create @path, *args
end

- (Boolean) directory?

Tests if this node is a directory.

Returns:

  • (Boolean)


62
63
64
# File 'lib/rumai/fs.rb', line 62

def directory?
  exist? and stat.directory?
end

- (Object) each(&block)

Iterates through each child of this directory.



157
158
159
# File 'lib/rumai/fs.rb', line 157

def each &block
  children.each(&block)
end

- (Object) each_line {|line| ... }

Invokes the given block for every line in the content of this node.

Yield Parameters:



100
101
102
103
104
105
106
# File 'lib/rumai/fs.rb', line 100

def each_line &block
  open do |file|
    until (chunk = file.read(true)).empty?
      chunk.each_line(&block)
    end
  end
end

- (Object) entries

Returns the names of all files in this directory.



69
70
71
72
73
74
75
# File 'lib/rumai/fs.rb', line 69

def entries
  begin
    IXP_AGENT.entries @path
  rescue IXP::Error
    []
  end
end

- (Boolean) exist?

Tests if this node exists on the IXP server.

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/rumai/fs.rb', line 51

def exist?
  begin
    true if stat
  rescue IXP::Error
    false
  end
end

- (Object) open(mode = 'r', &block)

Opens this node for I/O access.



82
83
84
# File 'lib/rumai/fs.rb', line 82

def open mode = 'r', &block
  IXP_AGENT.open @path, mode, &block
end

- (Object) parent

Returns the parent node of this node.



141
142
143
# File 'lib/rumai/fs.rb', line 141

def parent
  @@cache[ File.dirname(@path) ]
end

- (Object) read(*args)

Returns the entire content of this node.



91
92
93
# File 'lib/rumai/fs.rb', line 91

def read *args
  IXP_AGENT.read @path, *args
end

- (Object) remove

Deletes the file corresponding to this node on the IXP server.



127
128
129
# File 'lib/rumai/fs.rb', line 127

def remove
  IXP_AGENT.remove @path
end

- (Object) stat

Returns file statistics about this node.



44
45
46
# File 'lib/rumai/fs.rb', line 44

def stat
  IXP_AGENT.stat @path
end

- (Object) write(content)

Writes the given content to this node.



111
112
113
# File 'lib/rumai/fs.rb', line 111

def write content
  IXP_AGENT.write @path, content
end