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.



80
81
82
83
84
85
86
# File 'lib/rash/ext/filesystem.rb', line 80

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

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



74
75
76
# File 'lib/rash/ext/filesystem.rb', line 74

def children
  @children
end

#parentObject (readonly)

Returns the value of attribute parent.



74
75
76
# File 'lib/rash/ext/filesystem.rb', line 74

def parent
  @parent
end

Class Method Details

.root(dir) ⇒ Object



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

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

Instance Method Details

#add_child(path) ⇒ Object



127
128
129
130
131
# File 'lib/rash/ext/filesystem.rb', line 127

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

#add_local_method(name, &block) ⇒ Object



133
134
135
136
137
# File 'lib/rash/ext/filesystem.rb', line 133

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

#add_parent(dir) ⇒ Object



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

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

#child(path) ⇒ Object



121
122
123
124
125
# File 'lib/rash/ext/filesystem.rb', line 121

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



140
141
142
143
# File 'lib/rash/ext/filesystem.rb', line 140

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

#local_method(name) ⇒ Object



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

def local_method(name)
  @local_methods[name.to_sym]
end

#local_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/rash/ext/filesystem.rb', line 107

def local_method?(name)
  @local_methods.key?(name.to_sym)
end

#local_methodsObject



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

def local_methods
  @local_methods.keys
end

#lock_method(name) ⇒ Object

Raises:

  • (NameError)


100
101
102
103
104
105
# File 'lib/rash/ext/filesystem.rb', line 100

def lock_method(name)
  n = name.to_sym
  raise NameError.new("#{name} is not a local method", n) unless @local_methods.key?(n)
  @locked_methods << n unless @locked_methods.include?(n)
  n
end

#root?Boolean

Returns:

  • (Boolean)


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

def root?
  parent.nil?
end

#to_sObject



145
146
147
# File 'lib/rash/ext/filesystem.rb', line 145

def to_s
  @path.path
end

#unlocked_local_methodsObject



96
97
98
# File 'lib/rash/ext/filesystem.rb', line 96

def unlocked_local_methods
  @local_methods.filter{|k, v| !@locked_methods.include?(k)}
end