Class: Environment::Directory

Inherits:
Object
  • Object
show all
Defined in:
lib/rash/ext/filesystem.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, dir) ⇒ Directory

Returns a new instance of Directory.



75
76
77
78
79
80
# File 'lib/rash/ext/filesystem.rb', line 75

def initialize(parent, dir)
  @path = Dir.new(dir)
  @parent = parent
  @children = []
  @local_methods = parent&.local_methods.dup || {}
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



69
70
71
# File 'lib/rash/ext/filesystem.rb', line 69

def children
  @children
end

#local_methodsObject (readonly)

Returns the value of attribute local_methods.



68
69
70
# File 'lib/rash/ext/filesystem.rb', line 68

def local_methods
  @local_methods
end

#parentObject (readonly)

Returns the value of attribute parent.



69
70
71
# File 'lib/rash/ext/filesystem.rb', line 69

def parent
  @parent
end

Class Method Details

.root(dir) ⇒ Object



71
72
73
# File 'lib/rash/ext/filesystem.rb', line 71

def self.root(dir)
  Directory.new(nil, dir)
end

Instance Method Details

#add_child(path) ⇒ Object



98
99
100
101
102
# File 'lib/rash/ext/filesystem.rb', line 98

def add_child(path)
  dir = Directory.new(self, path)
  @children << dir
  dir
end

#add_local_method(name, &block) ⇒ Object



104
105
106
107
108
# File 'lib/rash/ext/filesystem.rb', line 104

def add_local_method(name, &block)
  raise ArgumentError.new "no method body provided" unless block_given?
  @local_methods[name] = block # if name already exists, its function is overriden
  name
end

#add_parent(dir) ⇒ Object



86
87
88
89
90
# File 'lib/rash/ext/filesystem.rb', line 86

def add_parent(dir)
  @parent = Directory.root(dir)
  @parent.add_child(self.to_s)
  @parent
end

#child(path) ⇒ Object



92
93
94
95
96
# File 'lib/rash/ext/filesystem.rb', line 92

def child(path)
  ch = @children.find {|c| c.to_s == path}
  ch = add_child(path) unless ch
  ch
end

#clear_local_method(name) ⇒ Object

might not be useful



111
112
113
114
# File 'lib/rash/ext/filesystem.rb', line 111

def clear_local_method(name)
  @local_methods.delete(name)
  name
end

#root?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/rash/ext/filesystem.rb', line 82

def root?
  parent.nil?
end

#to_sObject



116
117
118
# File 'lib/rash/ext/filesystem.rb', line 116

def to_s
  @path.path
end