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.



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

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

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

.root(dir) ⇒ Object



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

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

Instance Method Details

#add_child(path) ⇒ Object



235
236
237
238
239
# File 'lib/rash/ext/filesystem.rb', line 235

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

#add_local_method(name, &block) ⇒ Object



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

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



223
224
225
226
227
# File 'lib/rash/ext/filesystem.rb', line 223

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

#child(path) ⇒ Object



229
230
231
232
233
# File 'lib/rash/ext/filesystem.rb', line 229

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



157
158
159
160
# File 'lib/rash/ext/filesystem.rb', line 157

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

#clear_local_variable(name) ⇒ Object



210
211
212
213
# File 'lib/rash/ext/filesystem.rb', line 210

def clear_local_variable(name)
  @local_variables.delete(name.to_sym)
  name.to_sym
end

#local_method(name) ⇒ Object

Local method methods



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

def local_method(name)
  name = name.to_sym
  @local_methods[name] || @parent&.unlocked_local_method(name)
end

#local_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/rash/ext/filesystem.rb', line 119

def local_method?(name)
  name = name.to_sym
  @local_methods.key?(name) || !!@parent&.unlocked_local_method?(name)
end

#local_methodsObject



124
125
126
# File 'lib/rash/ext/filesystem.rb', line 124

def local_methods
  @local_methods.keys + (@parent&.unlocked_local_methods).to_a
end

#local_variable(name) ⇒ Object

Local variable stuff



166
167
168
169
# File 'lib/rash/ext/filesystem.rb', line 166

def local_variable(name)
  name = name.to_sym
  @local_variables[name] || @parent&.unlocked_local_variable(name)
end

#local_variable?(name) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/rash/ext/filesystem.rb', line 171

def local_variable?(name)
  @local_variables.include?(name.to_sym) || !!@parent&.unlocked_local_variable?(name.to_sym)
end

#local_variablesObject



175
176
177
# File 'lib/rash/ext/filesystem.rb', line 175

def local_variables
  @local_variables.keys + (@parent&.unlocked_local_variables).to_a
end

#lock_method(name) ⇒ Object

Raises:

  • (NameError)


149
150
151
152
153
154
# File 'lib/rash/ext/filesystem.rb', line 149

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

#lock_variable(name) ⇒ Object

Raises:

  • (NameError)


203
204
205
206
207
208
# File 'lib/rash/ext/filesystem.rb', line 203

def lock_variable(name)
  n = name.to_sym
  raise NameError.new("#{name} is not a local variable", n) unless @local_variables.key?(n)
  @locked_variables << n unless @locked_variables.include?(n)
  n
end

#root?Boolean

Generic traversal methods

Returns:

  • (Boolean)


219
220
221
# File 'lib/rash/ext/filesystem.rb', line 219

def root?
  parent.nil?
end

#set_local_variable(name, value) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/rash/ext/filesystem.rb', line 179

def set_local_variable(name, value)
  name = name.to_sym
  if !@local_variables.key?(name) && @parent&.unlocked_local_variable?(name)
    @parent&.set_local_variable(name, value)
  else
    @local_variables[name] = value
  end
end

#to_sObject



241
242
243
# File 'lib/rash/ext/filesystem.rb', line 241

def to_s
  @path.path
end

#unlocked_local_method(name) ⇒ Object



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

def unlocked_local_method(name)
  name = name.to_sym
  @local_methods[name] || @parent&.unlocked_local_method(name)
end

#unlocked_local_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def unlocked_local_method?(name)
  name = name.to_sym
  @local_methods.filter{|k, v| !@locked_methods.include?(k)}.key?(name) ||
    !!@parent&.unlocked_local_method?(name)
end

#unlocked_local_methodsObject



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

def unlocked_local_methods
  @local_methods.filter{|k, v| !@locked_methods.include?(k)}.keys + (@parent&.unlocked_local_methods).to_a
end

#unlocked_local_variable(name) ⇒ Object



188
189
190
191
# File 'lib/rash/ext/filesystem.rb', line 188

def unlocked_local_variable(name)
  name = name.to_sym
  @local_variables.filter{|k| !@locked_variables.include?(k)}[name] || @parent&.unlocked_local_variable(name)
end

#unlocked_local_variable?(name) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
200
201
# File 'lib/rash/ext/filesystem.rb', line 197

def unlocked_local_variable?(name)
  name = name.to_sym
  @local_variables.filter{|k,_v| !@locked_variables.include?(k)}.key?(name) ||
    !!@parent&.unlocked_local_variable?(name)
end

#unlocked_local_variablesObject



193
194
195
# File 'lib/rash/ext/filesystem.rb', line 193

def unlocked_local_variables
  @local_variables.keys.filter{|k| !@locked_variables.include?(k)} + (@parent&.unlocked_local_variables).to_a
end