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 = parent&.unlocked_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



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

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

#add_local_method(name, &block) ⇒ Object



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

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



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

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

#child(path) ⇒ Object



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

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



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

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

#clear_local_variable(name) ⇒ Object



177
178
179
180
# File 'lib/rash/ext/filesystem.rb', line 177

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

#local_method(name) ⇒ Object



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

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

#local_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#local_methodsObject



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

def local_methods
  @local_methods.keys
end

#local_variable(name) ⇒ Object



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

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

#local_variable?(name) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/rash/ext/filesystem.rb', line 138

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

#local_variablesObject



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

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

#lock_method(name) ⇒ Object

Raises:

  • (NameError)


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

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)


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

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

Returns:

  • (Boolean)


182
183
184
# File 'lib/rash/ext/filesystem.rb', line 182

def root?
  parent.nil?
end

#set_local_variable(name, value) ⇒ Object



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

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



216
217
218
# File 'lib/rash/ext/filesystem.rb', line 216

def to_s
  @path.path
end

#unlocked_local_methodsObject



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

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

#unlocked_local_variable(name) ⇒ Object



159
160
161
162
# File 'lib/rash/ext/filesystem.rb', line 159

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)


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

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



155
156
157
# File 'lib/rash/ext/filesystem.rb', line 155

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