Class: Rumai::Node

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

Overview

An entry in the IXP file system.

Direct Known Subclasses

Barlet, WidgetNode

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ExportInstanceMethods

extended

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

#pathObject (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

#childrenObject

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

#clearObject

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.

See Also:



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.

Returns:

  • (Boolean)

See Also:



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.

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

#entriesObject

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

#exist?Boolean

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

#open(mode = 'r', &block) ⇒ Object

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

#parentObject

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.

See Also:



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

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

#removeObject

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

#statObject

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

#write(content) ⇒ Object

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