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

Barlet, WidgetNode

Constant Summary

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Node) initialize(path)

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

- (Object) method_missing(meth, *args)

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

- (Object) path (readonly)

Returns the value of attribute path



30
31
32
# File 'lib/rumai/fs.rb', line 30

def path
  @path
end

Instance Method Details

- (Object) [](sub_path)

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

- (Object) children

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

- (Object) clear

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

- (Object) create(*args)

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

See Also:



124
125
126
# File 'lib/rumai/fs.rb', line 124

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

- (Boolean) directory?

Tests if this node is a directory.

Returns:

  • (Boolean)

See Also:



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

def directory?
  exist? and stat.directory?
end

- (Object) each(&block)

Iterates through each child of this directory.



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

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:

Raises:

  • (ArgumentError)


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

- (Object) entries

Returns the names of all files in this directory.

See Also:



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

- (Boolean) exist?

Tests if this node exists on the IXP server.

Returns:

  • (Boolean)


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

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

Opens this node for I/O access.

See Also:



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

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

- (Object) parent

Returns the parent node of this node.



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

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

- (Object) read(*args)

Returns the entire content of this node.

See Also:



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

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

- (Object) remove

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

See Also:



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

def remove
  IXP_AGENT.remove @path
end

- (Object) stat

Returns file statistics about this node.

See Also:



41
42
43
# File 'lib/rumai/fs.rb', line 41

def stat
  IXP_AGENT.stat @path
end

- (Object) write(content)

Writes the given content to this node.

See Also:



115
116
117
# File 'lib/rumai/fs.rb', line 115

def write content
  IXP_AGENT.write @path, content
end